Bulk selects using consistent sized batches
What I want to achieve is:
- Provide a mechanism for batch selects (
select * from use开发者_运维百科rs where user_id in (?, ?, ?, ?, ?)
). Batch size is 5 in this e.g. - Use bind variables and maintain consistent sized batches
So if the application requires Users for 8 different user_id then I would call the aforementioned SQL twice having 5 params in each call (rather than one call with 5 user_ids and then with 3).
So, I need to pad the user_ids if the input size is less than the batch size. I wanted to know what would be a good padding scheme in terms of performance:
- Using null?
- Using the first user_id
When using null would there be any performance degradation if the column is NULLABLE? Also when using null would the DB still come up with one query plan, because that's what I want to achieve using consistent sized batches?
Both of your options are equivalent*.
* Using null?
* Using the first user_id
There is no difference*. In an IN clause, NULLs are removed, so there is no performance degradation, UNLESS the RDBMS you are using has an ANSI_NULLS option that you can set to OFF (e.g. SQL Server) - see far below. In CLAUSES are also SETs, so having (1,2,3,1,1) is equivalent to only having (1,2,3) but it does make your binding easier.
So if you are (1) using a RDBMS that allows for NULLs to match NULLs and (2) you plan to use that setting, the 2 are equivalent.
On the other hand, to be 100% certain, just use the 2nd option.
SQL Server code showing NULL picking out NULLs.
set ansi_nulls off
select *
from (select 1 a union all select 2 union all select null) x
where a in (1,2, null)
精彩评论