Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Is Query of Queries faster than multiple WHERE criteria?

Engaged ,
Aug 12, 2009 Aug 12, 2009

If I need a recordset based on two table column fields (let's say firstName and lastName), is it better to run one query with a WHERE firstName = x AND lastName = y, or is it better to query of queries and find all records with a specified firstName, and then query that query to filter only records with a specified last name?  Which is the better/faster method?

<cfquery name="somequery" datasource="someds">

SELECT *

FROM sometable

WHERE firstName = 'John' AND lastName = 'Smith'

</cfquery>

or....

<cfquery name="somequery" datasource="someds">

SELECT *

FROM sometable

WHERE firstName = 'John'

</cfquery>

<cfquery name="somequery2" dbtype="query">

SELECT *

FROM somequery

WHERE lastName = 'Smith'

</cfquery>

TOPICS
Database access
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 12, 2009 Aug 12, 2009

It depends on a variety of things, but usually the fewer trips you make to the db, the better.  Also, the less data you bring back, the better.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 13, 2009 Aug 13, 2009

As a rule of thumb, make the database server do all the work... i.e. locally.  If what you are interested is "first name = x and last name = y," then those are the only records that you want to be "sent over the little wire" to you.  "The little wire" is always the bottleneck.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 13, 2009 Aug 13, 2009

Thanks guys.

What might be an example of when I'd like to use QoQ?  If anyone knows off-hand.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2009 Aug 13, 2009
LATEST

When combining data from more than one datasource.

When cfoutput query="yourquery" group="somefield" isn't going to get the job done..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources