Search Postgresql Archives

query time

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have a table with about 200.000 entries. Among other things, it
contains an integer field I use as a timestamp, and a variable
character field I use for user names. Certain queries are taking too
long IMO. I'm trying this on both 7.4 and 8.0.

If I do a direct comparison (using =) on the user name field and I
sort by the numeric field, I get about 5 ms. If I do a LIKE on the
user name and I don't sort at all, I get about 5 ms too. But if I use
both LIKE on the user name and sorting on the timestamp, the time
jumps to 2000 ms.

I have indexes on both fields, but I remember reading only one of them
will be used when doing a query.

Is such a serious jump in query times normal or am I doing something wrong?

I'm attaching the explain output for all 3 cases.
explain analyze select * from log_entries where user_name='john_doe' order by timestamp desc limit 100 offset 0;
                                                                            QUERY PLAN                                                                            
------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..235.47 rows=100 width=175) (actual time=0.945..5.858 rows=100 loops=1)
   ->  Index Scan Backward using timestamp_log_entries_key on log_entries  (cost=0.00..39093.47 rows=16602 width=175) (actual time=0.938..5.622 rows=100 loops=1)
         Filter: ((user_name)::text = 'john_doe'::text)
 Total runtime: 6.175 ms
(4 rows)



explain analyze select * from log_entries where user_name like '%john_doe%' limit 100 offset 0;
                                                    QUERY PLAN                                                    
------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..8250.17 rows=1 width=175) (actual time=0.495..3.364 rows=100 loops=1)
   ->  Seq Scan on log_entries  (cost=0.00..8250.17 rows=1 width=175) (actual time=0.486..3.138 rows=100 loops=1)
         Filter: ((user_name)::text ~~ '%john_doe%'::text)
 Total runtime: 3.657 ms
(4 rows)



explain analyze select * from log_entries where user_name like '%john_doe%' order by timestamp desc limit 100 offset 0;
                                                         QUERY PLAN                                                          
-----------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=8250.18..8250.19 rows=1 width=175) (actual time=1880.358..1880.910 rows=100 loops=1)
   ->  Sort  (cost=8250.18..8250.19 rows=1 width=175) (actual time=1880.345..1880.701 rows=100 loops=1)
         Sort Key: "timestamp"
         ->  Seq Scan on log_entries  (cost=0.00..8250.17 rows=1 width=175) (actual time=0.432..1051.036 rows=15481 loops=1)
               Filter: ((user_name)::text ~~ '%john_doe%'::text)
 Total runtime: 1887.071 ms
(6 rows)
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@xxxxxxxxxxxxxx)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux