12 Replies Latest reply: Apr 19, 2013 6:14 AM by BalddogRobin RSS

    Query of Search Results

    BalddogRobin Community Member

      I need some best practices advice.  I'm not sure if there is a better way to do what I'm trying to accomplish.  The execution time is very high which is why I'm looking for alternatives.  I welcome any tips to help reduce this execution time.  Hopefully, I can explain this clearly because I'm not seeing it clearly.  I'm going to just use one form field for this example, however, there are about 10 search field options.

       

      Multiple items (checkboxes) can be selected for one field.  I am going to use CODES for my example.

       

      The cfquery on the results page uses a view that has been created in the database.  This view joins a primary table (company) to several other tables (i.e., codes) that possibly have many records for that one company.  Within the cfquery, I use IN for the joined tables just to see if that company has any of those records.  It appears to be working and not taking too long.  In order to output the results, a  distinct company is being used because multiple rows could be returned for one company, and I only want to output each company one time. 

       

      Then, I want to output the company information.  I also want to output the multiple codes for returned for each company.  So, I have to do a little query within the main query output to get those records.  Because there are so many search field options, this is where I believe the execution time is slowing down because is has to loop very every company returned (800+) and multiple search fields means different queries.  Below is a sample piece of the code.

       

      I am thankful and open to any ideas.  Sincerely.

       

      <cfquery datasource="xxx" name="Company">

      Select distinct company

      from vwCompany

      where active='true'

      cfif isdefined("code") and code neq ''> and code IN (<cfqueryparam value="#form.code#" list="yes">)</cfif>

      </cfquery> 

       

      <cfoutput query="company">

       

      <CFQUERY DATASOURCE="xxx" NAME="getCodes">

          SELECT     *

          FROM     tblCodes

          WHERE code IN (<cfqueryparam value="#form.code#">)

      </CFQUERY>

       

       

      #company#

      codes: #valuelist(getcodes.code)#

       

      </cfquery>

        • 1. Re: Query of Search Results
          meensi Community Member

          You can try to join the queries and get the result as one query.

           

          Using queries of query, you can display the result for each company.

           

          Your first query will return all the results. Second query will use the first query(queries of query) and get the results for each company.

          • 2. Re: Query of Search Results
            BalddogRobin Community Member

            I've recently been coming across "query of queries," but I'm unfamiliar with it.

             

            I will research it.  Thank you so much.

            • 3. Re: Query of Search Results
              BalddogRobin Community Member

              Query of queries is a great tool to have.

               

              As I'm testing it, I've become stumped.

               

              How do I avoid having the query of query inside a loop to get the records to output for each company?  Am I doing this wrong?

               

              Using the same query as above (primary query)

               

              <cfloop query="companies">

               

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

              Select codes

              from company

              where companyid= <cfqueryparam value="#company.companyid#" cfsqltype="cf_sql_integer">

              </cfquery>

               

              <cfoutput>#company# #valuelist(codes.code)#

               

              </cfloop>

              • 4. Re: Query of Search Results
                Dan Bracuk Community Member

                The best answer, as stated earlier, is to figure out a way to get all your data with a single query by joining tables.  However, if that is not appropriate for the situation at hand, something else to look at the the sql keyword "in" along with the ColdFusion valuelist.  I'd give you an example but for some reason I can't use carraige returns on this site right now.

                • 5. Re: Query of Search Results
                  Aegis Kleais Community Member

                  To give you a short n sweet on QoQ, lets say I have a master query:

                   

                  <cfquery name="allTheData" datasource="myDSN" cachewithin="#createTimeSpan( 0, 1, 0, 0 )#">

                       SELECT

                            p.FirstName,

                            p.LastName

                       FROM Persons AS P

                       ORDER BY P.LastName ASC,

                            P.FirstName ASC

                  </cfquery>

                   

                  This queries the database for all the records, and then stores it into a server-controlled query cache for 1 hour (cachewithin attribute).  From there, I can perform a query on that cached query which runs at BLAZINGLY fast speeds, like

                   

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

                       SELECT * FROM allTheData WHERE FirstName = 'John'

                  </cfquery>

                   

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

                       SELECT * FROM allTheData WHERE LastName LIKE 'J%'

                  </cfquery>

                   

                  These 2 queries are run against the cached first query (allTheData) and come back VERY quickly (cause they don't hit the original database).  Just be careful!  You don't want to cache all the data if there is MASSIVE amounts of data or if that data changes frequently (as it'll take up lots of processing time, server resources and can become outdated)

                  • 6. Re: Query of Search Results
                    Dan Bracuk Community Member

                    You don't have to cache a query to make it available for query of queries.

                    • 7. Re: Query of Search Results
                      Aegis Kleais Community Member

                      Yeah, you don't.  You cache a query because in the event the query pulls a lot of data that doesn't change often, you can increase the performance of the retrieval by pulling from the ColdFusion query cache rather than hitting the RDBMS again for the same data.

                      • 8. Re: Query of Search Results
                        BalddogRobin Community Member

                        Thank you all for your input.  As a follow-up to what seems to have helped and to help others is a description below.

                         

                        I was having trouble getting an accurate recordcount of companies using the primary query (search results) because it was creating rows for the joined tables with a one-to-multi relationship.  It was accurately counting all the rows (multiple codes) returned and not just the distinct field (company) that I needed.

                         

                        I worked around it by using query of queries to get the accurate recordcount off of that primary query (companies used in previous example which I will rename as getcompanies since it is the same name as a table column).

                         

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

                        Select distinct company

                        from getcompanies

                        </cfquery>

                         

                        I then output each company record and their associated codes by using another QoQ:

                         

                        <cfoutput query="getcompanies">

                         

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

                        Select codes

                        from getcompanies

                        where companyid= <cfqueryparam value="#getcompanies.companyid#" cfsqltype="cf_sql_integer">

                        and code is not null

                        </cfquery>

                         

                        #company# Codes:</strong> #valuelist(getcodes.codes, ", ")#

                         

                        </cfoutput>

                         

                        If anyone knows of a more efficient way, I always welcome the suggestions.  It seems to have reduced the execution time from 16K ms to 6K ms, but I'm still trying to get the execution time down to under 250 ms.  There are only 800 records.

                        • 9. Re: Query of Search Results
                          Dan Bracuk Community Member

                          The answer might lie in your getcompanies query.  It could be as simple as doing a select distinct.

                          • 10. Re: Query of Search Results
                            BalddogRobin Community Member

                            There is a select distinct in it, but it still returns those companies multiple times if there have more than one code.

                             

                            <cfquery datasource="xxx" name="Company">

                            Select distinct company, codes

                            from vwCompany

                            where active='true'

                            cfif isdefined("code") and code neq ''> and code IN (<cfqueryparam value="#form.code#" list="yes">)</cfif>

                            </cfquery> 

                            • 11. Re: Query of Search Results
                              meensi Community Member

                              <cfquery datasource="xxx" name="Company">

                              Select distinct company, codes

                              from vwCompany

                              where active='true'

                              cfif isdefined("code") and code neq ''> and code IN (<cfqueryparam value="#form.code#" list="yes">)</cfif>

                              </cfquery> 

                               

                              from this you can take distinct compnay and get the codes accordingly.

                               

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

                                   select distinct company from company

                              </cfquery>

                               

                              <cfloop from =1 to =#getDiscompany.recordcount# index = i>

                                  

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

                                   select distinct codes from company where company = #getDisCompany.Company[i]#

                              </cfquery>

                              </cfloop>

                              • 12. Re: Query of Search Results
                                BalddogRobin Community Member

                                Thanks to all for your support and help!  It's one of the many reasons I've stuck with ColdFusion for so many years!