Is it more performant to use a Prepared Statement with one question mark in it fifty times, or to use a Prepared Statement with fifty question marks in it once?
Essentially is Where Person = ? or Where Person IN (?, ?, ?, ...) better?
Example
Say you have a table with a column, country, and then a few relational tables away you have the population for that country.
Given a list of 1000 countries, what is the best way to go about getting the population?
Keep in mind this is a hypothetical example, Wikipedia puts the number of countries at 223, let’s assume for this example it is much larger.
-
Create a statement that takes in a
country parameter and returns a population.
Example:Where Country = ? -
Create a Prepared Statement
dynamically, adding a ? for each
country using aWhere inclause. Example:
(?,?,etc)Where Country = (?, ?, ...) -
Create a
simple statement like in option
one, but loop through and reuse the
one parameter Prepared Statement for each
country.
What is the preferable method?
I reached a point in my project were I was able to test with some real data.
Based on 1435 items, Option 1 takes ~8 minutes, Option 2 takes ~15 seconds, and Option 3 takes ~3 minutes.
Option 2 is the clear winner in terms of performance. It is a little harder to code around, but the performance difference is too great to ignore.
I makes sense that going back and forth to the database is the bottleneck, though I’m sure the results listed here would vary based on network, database engine, database machine specs, and other environmental factors.