如何快速搜索查询
我有一个应用程序,其中几乎所有内容都是动态的。我正在为用户创建一个编辑表单,本质上需要搜索查询以选择一组复选框。
我有一个表,将用户分配给保存 userid 和 programid 的程序,这些程序映射到用户表和程序表中的相应记录。最初,我获取一个用户和所有程序,然后循环程序查询以构建复选框。
<cfloop query="Rc.programs">
<dd><input type="checkbox" name="programs" value="#Rc.programs.id#" /> #Rc.programs.name#</dd>
</cfloop>
我理想的做法是提取计划成员资格表中的所有记录,并对其进行某种搜索。我可以执行查询的查询,但我想知道是否有更快的方法来本质上搜索查询。如果这有助于人们理解,我的查询将如下所示。
从 Rc.programs 中选择*,其中programid = #Rc.programs.id#
I have an application where almost everything is dynamic. I am creating an edit form for a user and essentially need to search a query to select a group of checkboxes.
I have a table assigning the user to programs that holds userid and programid which maps to the corresponding records in the users table and the programs table. Initially I grab one user and all the programs and I loop over the programs query to build the checkboxes.
<cfloop query="Rc.programs">
<dd><input type="checkbox" name="programs" value="#Rc.programs.id#" /> #Rc.programs.name#</dd>
</cfloop>
What I ideally want to do is pull all records in the program memberships table and do some sort of search through that. I could do a query of queries, but I was wondering if there was a faster way to essentially search a query. My query of queries would be like the following if this helps people understand.
SELECT * FROM Rc.programs WHERE programid = #Rc.programs.id#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QoQ 当然是最简单的方法,但不要忘记 CFQUERYPARAM:
您还可以将查询的单个列/字段引用为数组,并使用数组函数(包括 arrayFind())仅搜索该列(其中可能只是最近的版本)。
如果这还不够快,您总是可以在内存中构建某种数据结构或索引,并将其保存在应用程序范围变量中(如果合适)。
但你的数据库真的那么慢吗?减少页面执行的查询数量几乎总是一件好事,但对于一般的、简单的查询,您可能无法击败数据库服务器的速度、缓存等。
QoQ is certainly the easiest way to do it, but don't forget your CFQUERYPARAM:
You can also reference an individual column/field of a query as an array, and search through just that column using array functions, including arrayFind() (which might just be in recent versions).
If that's not fast enough you could always build some sort of data structure or index in memory, and keep it around in an Application-scope variable if such is appropriate.
But is your database really that slow? Reducing the number of queries executed by a page is almost always a good thing, but for average, uncomplicated queries you probably won't be able to beat the speed, caching, etc of your DB server.