How to efficiently SELECT rows from database table based on selected set of values

Posted by Chau Chee Yang on Stack Overflow See other posts from Stack Overflow or by Chau Chee Yang
Published on 2010-04-24T05:03:05Z Indexed on 2010/04/24 5:13 UTC
Read the original article Hit count: 292

Filed under:
|
|

I have a transaction table of 1 million rows. The table has a field name "Code" to keep customer's ID. There are about 10,000 different customer code.

I have an GUI interface allow user to render a report from transaction table. User may select arbitrary number of customers for rendering.

I use IN operator first and it works for few customers:

SELECT * FROM TRANS_TABLE WHERE CODE IN ('...', '...', '...')

I quickly run into problem if I select few thousand customers. There is limitation using IN operator.

An alternate way is create a temporary table with only one field of CODE, and inject selected customer codes into the temporary table using INSERT statement. I may then using

SELECT A.* FROM TRANS_TABLE A INNER JOIN TEMP B ON (A.CODE=B.CODE)

This works nice for huge selection. However, there is performance overhead for temporary table creation, INSERT injection and dropping of temporary table.

Do you aware of better solution to handle this situation?

© Stack Overflow or respective owner

Related posts about sql

Related posts about Performance