Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 23rd, 2005, 02:23 AM
Tim Smith
Guest
 
Posts: n/a
Default SELECT from a set of values really slow?

I've been benchmarking some very simple databases. By simple, I mean a table
like this:

CREATE TABLE bench (
id SERIAL,
data TEXT )
CREATE INDEX bench_data_index ON bench (data)

which is filled with 100k records, where the data values for each record are
distinct (the data for record N is "text_item_N").

I generate a random data value known to be in the table and lookup its id,
with "SELECT id FROM bench WHERE data = 'whatever'".

I run this for three seconds. PostgreSQL does OK: 4300 selects in 2999
msec. MySQL does a little better, but not much: 5500 selects in 2999 msec.

In my application, this kind of lookup turns out to be the bottleneck on
performance (which is why I'm benchmarking). On MySQL, I can speed it up
quite a bit by looking up more than one thing at a time:

SELECT id FROM bench WHERE data IN ('X', 'Y')

for example, gets about 4050 selects in 3 seconds, so that's 8100 records
looked up, compared to 5300 when they were done one at a time. It continues
to improve selecting more at a time. To my surprise, when I tried this
trick with PostgreSQL, it did not speed things up. In fact, it *massively*
slowed down--it only is getting 13 selects in 3 seconds, searching for two
at a time.

What's going on here?

PostgreSQL 7.4.2 on SuSE 9.1 Linux. (The version that comes with SuSE).

--
--Tim Smith
  #2  
Old November 23rd, 2005, 02:24 AM
CoL
Guest
 
Posts: n/a
Default Re: SELECT from a set of values really slow?

hi,

Tim Smith wrote:[color=blue]
>
> What's going on here?
>[/color]

#>explain analyze SELECT id FROM bench WHERE data IN ('X', 'Y');
#>set enable_seqscan to off;
#>explain analyze SELECT id FROM bench WHERE data IN ('X', 'Y');

result?

C.
  #3  
Old November 23rd, 2005, 02:28 AM
Tom Lane
Guest
 
Posts: n/a
Default Re: SELECT from a set of values really slow?

Tim Smith <reply_in_group@mouse-potato.com> writes:[color=blue]
> SELECT id FROM bench WHERE data IN ('X', 'Y')[/color]
[color=blue]
> To my surprise, when I tried this trick with PostgreSQL, it did not
> speed things up. In fact, it *massively* slowed down--it only is
> getting 13 selects in 3 seconds, searching for two at a time.[/color]
[color=blue]
> What's going on here?[/color]

Likely it's switching from index to sequential scan because of a poor
estimate of how many rows will be returned. Have you ever ANALYZEd
the test table? Without either ANALYZE stats or a unique index,
the planner will certainly not think that the column is unique.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

  #4  
Old November 23rd, 2005, 02:28 AM
Pierre-Frédéric Caillaud
Guest
 
Posts: n/a
Default Re: SELECT from a set of values really slow?

test=> insert into bench (id,data) select id, 'text_item_'||id::text from
dummy where id<=100000 order by id;
INSERT 0 100001
test=> CREATE INDEX bench_data_index ON bench (data);
CREATE INDEX
test=> explain select * from bench where data = 'test_item_1';
Index Scan using bench_data_index on bench (cost=0.00..1478.85 rows=501
width=36)
Index Cond: (data = 'test_item_1'::text)
(2 lignes)

test=> explain select * from bench where data in ( 'test_item_1',
'test_item_2' );
Seq Scan on bench (cost=0.00..2190.01 rows=998 width=36)
Filter: ((data = 'test_item_1'::text) OR (data = 'test_item_2'::text))
(2 lignes)

test=> vacuum analyze bench;
VACUUM
test=> explain select * from bench where data in ( 'test_item_1',
'test_item_2' );
Index Scan using bench_data_index, bench_data_index on bench
(cost=0.00..7.91 rows=2 width=22)
Index Cond: ((data = 'test_item_1'::text) OR (data =
'test_item_2'::text))
(2 lignes)

=> once you analyze, it works...


***********************************************

With 1 item :

test=>explain analyze select * from bench where data in ( 'test_item_1' );
Total runtime: 0.127 ms

With 11 items :

test=>explain analyze select * from bench where data in ( 'test_item_1',
'test_item_2', 'test_item_55', 'test_item_64', 'test_item_1005',
'test_item_78541', 'test_item_96521', 'test_item_8574', 'test_item_89652',
'test_item_14527', 'test_item_48652' );
Total runtime: 0.352 ms

***********************************************

With a Join... see on psql-performance



On Sun, 10 Oct 2004 16:00:10 -0400, Tom Lane <tgl@sss.pgh.pa.us> wrote:
[color=blue]
> Tim Smith <reply_in_group@mouse-potato.com> writes:[color=green]
>> SELECT id FROM bench WHERE data IN ('X', 'Y')[/color]
>[color=green]
>> To my surprise, when I tried this trick with PostgreSQL, it did not
>> speed things up. In fact, it *massively* slowed down--it only is
>> getting 13 selects in 3 seconds, searching for two at a time.[/color]
>[color=green]
>> What's going on here?[/color]
>
> Likely it's switching from index to sequential scan because of a poor
> estimate of how many rows will be returned. Have you ever ANALYZEd
> the test table? Without either ANALYZE stats or a unique index,
> the planner will certainly not think that the column is unique.
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faqs/FAQ.html
>[/color]



---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles