Solved

Best practice for CFQuery: using DBName as attribute or in SQL?

Posted on 2008-10-28
7
688 Views
Last Modified: 2012-06-27
When using CFQuery, is it best to have the database name as an attribute (example code 1) or as part of the SQL (example code 2)?  

Does this make a difference for ColdFusion query caching (cachedWithin)?

(currently using ColdFusion 8)
example 1:
<cfquery name="qCountry" datasource="LOCALDSN" dbname="#curr_db_name#">
SELECT * FROM tblCountry WHERE country_id = #cid#
</cfquery
 
example 2
<cfquery name="qCountry" datasource="LOCALDSN">
SELECT * FROM #curr_db_name#.dbo.tblCountry WHERE country_id = #cid#
</cfquery

Open in new window

0
Comment
Question by:paid_tech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:erikTsomik
ID: 22822790
it does not really matter the datasource get setup in CF administrator which will point to the DB file. So what you doing is absolutely identicall and has no effect on the perforance
0
 
LVL 36

Accepted Solution

by:
SidFishes earned 125 total points
ID: 22822896
actually dbname is deprecated and should not be used

"Deprecated the connectString, dbName, dbServer, provider, providerDSN, and sql attributes, and all values of the dbtype attribute except query. They do not work, and might cause an error, in releases later than ColdFusion 5. "

from livedocs
0
 

Author Comment

by:paid_tech
ID: 22823575
thank you for pointing out the deprecation of dbname
(http://www.cfquickdocs.com/cf8/?getDoc=cfquery#cfquery)
just to clarify I have about 40 databases, one for each site, and they are all setup through the same ColdFusion Datasource/DSN.

1) if its possible to switch to a DSN for each database, should we change to that?
2) So should I use the format of example 2, with the dname in front of each table name in the SQL? (example below)

<cfquery name="qCountry" datasource="LOCALDSN">
SELECT * 
FROM #curr_db_name#.dbo.tblCountry  AS c
LEFT OUTER JOIN #curr_db_name#.dbo.tblCountryStatus AS cs ON c.country_id = cs.country_id
WHERE country_id = #cid#
</cfquery

Open in new window

0
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
LVL 36

Assisted Solution

by:SidFishes
SidFishes earned 125 total points
ID: 22823745
yes example 2 would be the best approach...i believe it's actually a bit better than multiple dsn's as it use the db's power rather than cf's and that's always a good thing

you could try putting the variable in application scope if each site has it's own codebase

FROM #application.curr_db_name#.dbo.tblCountry  AS c

0
 

Author Comment

by:paid_tech
ID: 22824209
thank you SidFish

also does this approach (with dbname in SQL) affect the ColdFusion query caching?

would ex 3 & 4 be considered different queries by ColdFusion, and hence be cached as different queries?

ex 3:
<cfquery name="qCountry" datasource="LOCALDSN">
SELECT * 
FROM funSite.dbo.tblCountry  AS c
LEFT OUTER JOIN funSite.dbo.tblCountryStatus AS cs ON c.country_id = cs.country_id
WHERE country_id = 5
</cfquery>
 
ex 4:
<cfquery name="qCountry" datasource="LOCALDSN">
SELECT * 
FROM testSite.dbo.tblCountry  AS c
LEFT OUTER JOIN testSite.dbo.tblCountryStatus AS cs ON c.country_id = cs.country_id
WHERE country_id = 5
</cfquery>

Open in new window

0
 
LVL 36

Assisted Solution

by:SidFishes
SidFishes earned 125 total points
ID: 22824626
"To pull from the cache, more than just the name of the query must match. Here's the list:

    * Same query "Name"
    * Exact same SQL statement - "where username='bubbaLouie'" and "where username = 'samIam'" are 2 different statements, ergo 2 different queries in the cache - even if they are both "named" NightOnTown.
    * Same Datasource - for those of you who fail to assume and stumbled onto that thought.
    * Same Username and password - This is interesting to note. If you have a site with a shared datasource but multiple db usernames you may not get the benefit from caching that you think you should.
    * Same DBTYPE"

http://mkruger.cfwebtools.com/index.cfm?mode=entry&entry=EAA0D1CA-01F6-F3EC-5520AAD6EEC68061


that being said, if you're not using (which your examples don't)

cachedwithin="#crateTimespan(0,010,0)#" in your cfquery tag you're not using caching anyways... (and caching has to be enabled in cfadmin)

for the benefit of future readers of this q, as noted in the article, versions prior to CF8 could not use cached queries -and- cfqueryparam. This is a major problem as imho, there is no circumstance where you should eliminate the use of cfqueryparam as protection agaisnt sql injection even if it means giving up server performance.




0
 

Author Closing Comment

by:paid_tech
ID: 31510776
Thank you very much, your answers were detailed and easy to understand
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question