查询查询react版本
Besides CFCs, one of my favorite aspects of ColdFusion is the Query of Queries, which was introduced in ColdFusion 5.x.
除了CFC ,ColdFusion我最喜欢的方面之一是查询查询,它是在ColdFusion 5.x中引入的。
What is a Query of Queries? According to the Macromedia Live Docs,
什么是查询查询? 根据Macromedia Live Docs ,
a query that retrieves data from a record set is called a Query of Queries. After you generate a record set, you can interact with its results as if they were database tables by using Query of Queries.
从记录集中检索数据的查询称为查询查询。
生成记录集后,可以使用查询查询将其结果与数据库表进行交互。
To me, a Query of Queries is the ability to re-query an existing record set. For example, let’s say you have an interface that shows you all the users in a system. You probably used a query similar to select * from users order by lastName
and then output them to your HTML somehow. Now let’s say you list the alphabet across the top of this output so that a user could click on ‘J’, for instance, to see everyone whose last name starts with ‘J’. You would have to re-query the database and use something like select * from users where lastName like 'j%' order by lastName
.
对我来说,查询查询是一种重新查询现有记录集的功能。 例如,假设您有一个界面,向您显示系统中的所有用户。 您可能使用了类似的查询, select * from users order by lastName
,然后以某种方式将其输出到HTML。 现在,假设您在此输出的顶部列出了字母,以便用户可以单击“ J”,例如,查看姓氏以“ J”开头的每个人。 您将不得不重新查询数据库并使用诸如select * from users where lastName like 'j%' order by lastName
类的东西select * from users where lastName like 'j%' order by lastName
。
The downside to this is that you’ve now sent two queries to your database and taken up that much more bandwidth. For a single request, this is nothing, but can you imagine using these queries on a site that gets thousands of hits a day. Pretty soon, you’re going to be looking at upgrading hardware or your network connection to handle the extra queries and load.
不利的一面是,您现在已经向数据库发送了两个查询,并占用了更多的带宽。 对于单个请求,这没什么,但是您可以想象在每天获得数千次点击的网站上使用这些查询。 很快,您将要升级硬件或网络连接,以处理额外的查询和负载。
减轻负担–行动查询 (Lightening the Load – the Query of Queries in Action)
Now, let’s look at this issue again. This time, though, we’ll use ColdFusion and the Query of Queries ability to lighten the load and increase the functionality of our script. Let’s assume our client has asked for a reporting feature that will show him the following:
现在,让我们再次看一下这个问题。 但是,这一次,我们将使用ColdFusion和查询查询功能来减轻负载并增加脚本的功能。 假设我们的客户要求提供一种报告功能,该功能将向他显示以下内容:
- Total number of registered users 注册用户总数
- Total number of male users 男性使用者总数
- Total number of female users 女性使用者总数
- Total number of male users at or over the age of 18 18岁或以上的男性使用者总数
- Total number of female users at or over the age of 18 18岁或以上的女性使用者总数
We’ll assume our table contains the following columns:
我们假设我们的表包含以下列:
fname
fname
lname
lname
gender
gender
age
age
email
email
We’ll use this table for all our queries and, since this is just a brief tutorial, we won’t go into details about the data types used or the semantics of SQL queries. In other words, I assume you can create the table, write basic SQL code, and understand basic SQL theory.
我们将使用此表进行所有查询,并且由于这只是一个简短的教程,因此我们不会详细介绍所使用的数据类型或SQL查询的语义。 换句话说,我假设您可以创建表,编写基本SQL代码并了解基本SQL理论。
要求1:注册用户总数 (Requirement 1: Total Registered Users)
Looking at our requirements, the first item the client wants is the total number of registered users. Our SQL will look like this:
根据我们的要求,客户需要的第一项是注册用户总数。 我们SQL将如下所示:
<cfquery name="allUsers" datasource="#APPLICATION.dbSource#">
select fname, lname, gender, age, email from users
</cfquery>
A quick side note here: when I set up an application, I typically store my database connection name in my Application.cfm file and assign it some application-level variable. Feel free to make this fit your coding needs and style.
这里有个简短的注释:设置应用程序时,通常将数据库连接名称存储在Application.cfm文件中,并为其分配一些应用程序级变量。 随意使其适合您的编码需求和风格。
Now, the above query will grab all the users in the database and return a record set named allUsers
. You’ll notice I didn’t do a Select COUNT(*) as count from users
query, but instead opted to select all the columns and place this data into the record set. I choose this route because it allows us to further expand the code if, in future, the client wants detailed lists or reports. If we just ran the count query, we’d have to make major modifications for future reports, as the actual user data was missing.
现在,上面的查询将获取数据库中的所有用户,并返回一个名为allUsers
的记录集。 您会注意到我没有Select COUNT(*) as count from users
查询中Select COUNT(*) as count from users
,而是选择了选择所有列并将此数据放入记录集中。 我选择此路线是因为,如果将来客户需要详细的列表或报告,它可以使我们进一步扩展代码。 如果仅运行计数查询,则由于实际的用户数据丢失,我们将不得不对以后的报告进行重大修改。
We have a record set that contains all our users. Now, we need to output this information to meet the clients’ request. We need to generate a report that tells the client the total number of users in the system.
我们有一个包含所有用户的记录集。 现在,我们需要输出此信息以满足客户的要求。 我们需要生成一个报告,以告知客户端系统中的用户总数。
Total users in system: <cfoutput>#allUsers.RecordCount#</cfoutput><br />
系统中的用户总数: <cfoutput>#allUsers.RecordCount#</cfoutput><br />
When placed into a ColdFusion HTML (.cfml or .cfm) page, the above code will generate a line of text showing our client the total number of users currently in the system. This will be displayed as a numerical value that’s automatically calculated and generated by the ColdFusion server as the variable RecordCount
.
当将上述代码放入ColdFusion HTML(.cfml或.cfm)页面时,将生成一行文本,向我们的客户端显示当前系统中用户的总数。 这将显示为由ColdFusion服务器自动计算和生成的数值,作为变量RecordCount
。
要求2:系统中的男性用户总数和女性用户总数 (Requirement 2: Total Male and Total Female Users in the System)
Now, we need to generate the output for the total number of male and female users in the system.
现在,我们需要生成系统中男性和女性用户总数的输出。
As we already have a record set that contains all current users, we will code a Query of Queries to generate the rest of the output. The new code is shown in bold below.
因为我们已经有了一个包含所有当前用户的记录集,所以我们将对查询查询进行编码以生成其余的输出。 新代码在下面以粗体显示。
<!--- grab all users --->
<cfquery name="allUsers" datasource="#APPLICATION.dbSource#">
select fname, lname, gender, age, email from users
</cfquery> <!--- grab all the men --->
<cfquery name="allMales" dbtype="query">
select * from allUsers where gender = 'male'
</cfquery>
<!--- grab all the women --->
<cfquery name="allFemales" dbtype="query">
select * from allUsers where gender = 'female'
</cfquery>
The above code is our first two Queries of Queries. To perform a Query of Queries, you must have a valid record set to query against. For this application, the valid record set is the allUsers
query / record set.
上面的代码是我们的前两个查询查询。 要执行查询,您必须具有要查询的有效记录集。 对于此应用程序,有效的记录集是allUsers
查询/记录集。
Now, we update the output with the newest code, again shown in bold.
现在,我们用最新的代码更新输出,再次以粗体显示。
Total users in system: <cfoutput>#allUsers.RecordCount#</cfoutput><br />Total male users in system: <cfoutput>#allMales.RecordCount#</cfoutput><br />
Total female users in system: <cfoutput>#allFemales.RecordCount#</cfoutput><br />
We need to further query the allMales
and allFemales
record set so we can report on the total number of users over the age of 18.
我们需要进一步查询allMales
和allFemales
记录集,以便我们可以报告18岁以上的用户总数。
要求3:每个性别的总用户数超过18 (Requirement 3: Total Users of Each Gender Over 18)
We change our SQL code portions to look like the following (new code in bold):
我们将SQL代码部分更改为如下所示(新代码以粗体显示):
<!--- grab all users --->
<cfquery name="allUsers" datasource="#APPLICATION.dbSource#">
select fname, lname, gender, age, email from users
</cfquery>
<!--- grab all the men --->
<cfquery name="allMales" dbtype="query">
select * from allUsers where gender = 'male'
</cfquery> <!--- grab all the men at or over age of 18 --->
<cfquery name="allMales18" dbtype="query">
select * from allMales where age >= 18
</cfquery>
<!--- grab all the women --->
<cfquery name="allFemales" dbtype="query">
select * from allUsers where gender = 'female'
</cfquery> <!--- grab all the women at over age of 18 --->
<cfquery name="allFemales18" dbtype="query">
select * from allFemales where age >= 18
</cfquery>
Now update your output code as follows:
现在,如下更新输出代码:
Total users in system: <cfoutput>#allUsers.RecordCount#</cfoutput><br />
Total male users in system: <cfoutput>#allMales.RecordCount#</cfoutput><br /> Total male users in system at or over age 18: /<cfoutput>#allMales18.RecordCount#</cfoutput><br />
Total female users in system: <cfoutput>#allFemales.RecordCount#</cfoutput><br /> Total female users in system at or over age 18: <cfoutput>#allFemales18.RecordCount#</cfoutput><br />
Let’s take a few moments to dive deeper into this code. The first thing you’d have noticed was that our Queries of Queries did not specify a datasource, but instead specified a database type query (dbtype="query"
). This tells the ColdFusion engine that we want to re-query an existing record set, thereby creating a Query of Queries. You’ll also notice that, in our select statement, we didn’t reference the table users. Instead, we referenced our original query, which, in the case of our first two Queries of Queries, was allUsers
. Note that we don’t specify the genders in the 18 and over searches. This condition was originally stipulated in our allMales
and allFemales
queries, so it doesn’t need to be applied again.
让我们花一些时间来深入研究此代码。 您会注意到的第一件事是,我们的查询查询没有指定数据源,而是指定了数据库类型查询( dbtype="query"
)。 这告诉ColdFusion引擎我们要重新查询现有记录集,从而创建查询查询。 您还将注意到,在我们的select语句中,我们没有引用表用户。 相反,我们引用了原始查询,对于我们的前两个查询,该查询为allUsers
。 请注意,我们没有在18个及以上的搜索中指定性别。 此条件最初是在我们的“所有allMales
和“所有allFemales
查询中规定的,因此无需再次应用。
轻载 (A Lighter Load)
There you have it — a very basic but powerful introduction into the world of Query of Queries.
到那里就可以了–对查询查询世界的一个非常基本但功能强大的介绍。
I’m sure you want to know how this lightens your load. Well, the performance increase is difficult to gauge without a database full of users with whom you can to perform tests. But if you stop and compare what the server must do with our Query of Queries code against the processing it would complete if we performed a new database query for each of these statistics, a performance increase is inevitable. Yes, ColdFusion must spend more time and memory retrieving the complete list of users from the database in the first query, but, in return, it gets to calculate statistics on the fly by simply juggling the record sets in memory, rather than going back to the database, which is always a costly process.
我确定您想知道这如何减轻您的负担。 好吧,如果没有一个充满用户可以与他们一起执行测试的数据库,就很难评估性能的提高。 但是,如果停止并比较服务器对我们的“查询查询”代码必须执行的操作与如果对这些统计信息中的每个统计信息执行新的数据库查询将完成的处理,则性能提升是不可避免的。 是的,ColdFusion必须花费更多的时间和内存来在第一个查询中从数据库中检索用户的完整列表,但是作为回报,它可以通过简单地处理内存中的记录集来动态计算统计信息,而不是返回到数据库,这总是一个昂贵的过程。
应用查询查询 (Applying Query of Queries)
When should you use Query of Queries? In my humble opinion, Query of Queries should be used when you can limit the number of connections to a database server without hampering the coding and design of your application. I don’t recommend you rush out and run 10 select * from Table
statements, using Queries of Queries throughout your application. Keep in mind that the result sets you create have to be stored somewhere. Most of the time, they’re stored in memory (be it RAM or virtual) and, if you don’t plan properly, your server could slow to a crawl or even stop because of low memory resources.
什么时候应该使用查询查询? 以我的拙见,当您可以在不影响应用程序的编码和设计的情况下限制与数据库服务器的连接数时,应使用“查询查询”。 我不建议您在整个应用程序中使用Queries of Queries select * from Table
语句中急速运行10 select * from Table
。 请记住,您创建的结果集必须存储在某个位置。 大多数情况下,它们存储在内存(RAM或虚拟内存)中,如果您没有正确计划,则由于内存资源不足,服务器可能会缓慢爬行甚至停止。
As with any project, it’s best to plan out the entire system first, then go back and refine it multiple times, as you seek ways to increase performance, security, and overall stability. It’s during this refinement phase that you can pinpoint possible areas in which Query of Queries will be beneficial for the applications and the system resources. I hope this article has given you the drive to look over your code and see how you can implement the query of queries functionality into your projects. If you need more help feel free to drop me a line, or post in the SitePoint Forums.
对于任何项目,最好先对整个系统进行规划,然后再进行多次优化,以寻求提高性能,安全性和整体稳定性的方法。 在此优化阶段,您可以确定可能的区域,其中“查询查询”将对应用程序和系统资源有利。 我希望本文能带给您驱动力来查看代码,并了解如何在项目中实现查询功能的查询。 如果您需要更多帮助,请随时给我留言,或在SitePoint论坛中发帖 。
查询查询react版本