Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 24, 2014 23:23
SQL IN Directive Much Faster Than Multiple OR Clauses
<!--- Query for the hit IDs. --->
<cfquery name="qIDs" datasource="..." username="..." password="...">
SELECT
h.id
FROM
web_stats_hit h
</cfquery>
<!--- Query for the ID records using the IN directive. --->
<cfquery name="qInTest" datasource="..." username="..." password="...">
SELECT
h.id,
h.date_created
FROM
web_stats_hit h
WHERE
<!---
In the ValueList(), I am passing in comments in an attempt
to make the amount of SQL larger so that it compares to the
next query which physically has more character data.
--->
h.id IN ( #ValueList( qIDs.id, ", /*..*/" )# )
</cfquery>
<!--- Query for the ID records using an OR clause for every ID. --->
<cfquery name="qOrTest" datasource="..." username="..." password="...">
SELECT
h.id,
h.date_created
FROM
web_stats_hit h
WHERE
1 = 1
<!---
Loop over IDs. I am doing a 'sloppy' loop here to create
as little white space as possible so as to not have to push
so much darn data to the SQL server.
--->
<cfloop query="qIDs">OR id=#qIDs.id# </cfloop>
</cfquery>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment