![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Would you expect this query: select * from product p where p.product_id in (select product_id from price) To be as fast as this one: select * from product p where p.product_id in (select product_id from price where product_id = p.product_id) (in general, or with the DBMS of your choice)? |
|
I am right in thinking the two are functionally equivalent, right? I would agree, yes. |
|
Out of interest, how would you expect these two to compare: select * from product p where exists (select * from price where product_id = p.product_id) select * from product p where exists (select 1 from price where product_id = p.product_id) |
#3
| |||
| |||
|
|
Tom Anderson, 19.05.2010 14:25: Would you expect this query: select * from product p where p.product_id in (select product_id from price) To be as fast as this one: select * from product p where p.product_id in (select product_id from price where product_id = p.product_id) (in general, or with the DBMS of your choice)? Yes I would expect the optimizer to detect that situation. As a matter of fact this is a very good example why FK constraints are useful beyond mere data integrity/constency. Oracle 11g produces identical plans for both version (so it detects the situation) |
#4
| |||
| |||
|
|
Yes I would expect the optimizer to detect that situation. As a matter of fact this is a very good example why FK constraints are useful beyond mere data integrity/constency. Oracle 11g produces identical plans for both version (so it detects the situation) db2 v9.5 fixpak 0 does as well. It rewrites both queries to: SELECT DISTINCT Q1.PRODUCT_ID AS PRODUCT_ID FROM PRICE AS Q1 But that is a completely different result! |
#5
| ||||||||
| ||||||||
|
|
I will fill the tables with some random data and see if it makes a difference. |
| 7424 |
| 7424 |
| 11765 |
| 11765 |
|
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
|
1 | PRIMARY | p | index | NULL | PRIMARY | 4 NULL | 1 | Using where; Using index | 2 | DEPENDENT SUBQUERY | price | index | NULL | PRIMARY | 6 NULL | 1 | Using where; Using index | +----+--------------------+-------+-------+---------------+---------+---------+------+------+--------------------------+ |
|
/Lennart |
#6
| |||
| |||
|
|
Lennart Jonsson wrote on 19.05.2010 16:55: Yes I would expect the optimizer to detect that situation. As a matter of fact this is a very good example why FK constraints are useful beyond mere data integrity/constency. Oracle 11g produces identical plans for both version (so it detects the situation) db2 v9.5 fixpak 0 does as well. It rewrites both queries to: SELECT DISTINCT Q1.PRODUCT_ID AS PRODUCT_ID FROM PRICE AS Q1 But that is a completely different result! In your examples you are retrieving the complete row from the product table, but this statement only returns product_ids |
#7
| |||
| |||
|
|
Hi all, Given tables that look a bit like: create table product ( product_id integer primary key -- other fields ) create table price ( country_code char(2) not null, product_id integer not null references product, constraint price_pk primary key (country_code, product_id) -- other fields ) Would you expect this query: select * from product p where p.product_id in (select product_id from price) To be as fast as this one: select * from product p where p.product_id in (select product_id from price where product_id = p.product_id) |
|
I would have thought that any query planner worth its salt would execute those queries the same way. My colleague insists that the subquery in the first version will force the engine to make a table scan over price. A quick experiment (with different but roughly corresponding queries) on SQL Server finds that the second version, with the where clause, is in fact faster. |
|
Out of interest, how would you expect these two to compare: |
![]() |
| Thread Tools | |
| Display Modes | |
| |