帮助从 MySQL 查询获取或显示 ColdFusion 中的随机记录吗?

发布于 2024-09-29 08:14:41 字数 277 浏览 7 评论 0 原文

我有一个 jquery 滚轮/滚动器,它显示从我的“有用提示”数据库表查询返回的记录片段。我想让滚动条保持在大约 15 条记录,但不总是前 15 条记录。

编写这样的查询是否更有效:

SELECT *
FROM table
ORDER BY RAND()
LIMIT n

哪个返回随机结果,还是返回整个查询并让我的 ColdFusion 组件提供查询结果的随机数?

我的滚动条的未来还将包括“待售物品”表中的随机记录,因此我需要牢记这一点。

I've got a jquery roller/scroller that displays snippets of records returned from my 'Helpful Hints' database table query. I want to keep the scroller to about 15 records but not always the first 15 records.

Is it more efficient to write a query like:

SELECT *
FROM table
ORDER BY RAND()
LIMIT n

Which returns a random result or do I return the whole query and have my ColdFusion component serve up a random number of the query result?

The future of my scroller will include random records from my 'Items for Sale' table as well, so I need to keep that in mind.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耀眼的星火 2024-10-06 08:14:41

我同意这一点,因为返回大量不用于 Coldfusion 的值结果集是一种永远无法收回的资源浪费。

但要小心使用 MySQL 的 RAND( ) 排序功能 - 一旦记录超过 100,000 条,它就无法很好地扩展 (参见图表),您应该看看使用替代方案

I agree with bemace that returning a large result set of values that won't be used to Coldfusion is a waste of resources that can never be recouped.

But be careful about using MySQL's RAND() function for ordering - once you're over 100,000 records, it doesn't scale well (see graph), and you should look at using alternatives.

红颜悴 2024-10-06 08:14:41

除非您要缓存相对较小的静态查询,否则在数据库端随机化记录通常会更有效。这样每次只提取 15 条记录,而不是表中的所有记录。

Unless you are caching a relatively small and static query, it is usually more efficient to randomize the records on the database side. That way you are only pulling 15 records each time, not all records in the table.

蓝眼泪 2024-10-06 08:14:41

返回整个结果集并扔掉大部分结果肯定会降低效率。

当您需要包含其他表中的数据时,您可以运行另一个查询(与您已有的查询类似),然后在 ColdFusion 中将结果混合在一起。

为了完整起见,类似这样的事情是可能的,尽管速度太慢而不实用:

( SELECT * FROM table1 )
UNION
( SELECT * FROM table2 )
ORDER BY RAND()
LIMIT 30 

Returning the entire result set and them throwing most of it out would definitely be less efficient.

When you get to the point of including data from other tables you can run another query like the one you've got and then shuffle the results together in ColdFusion.

For the sake of completeness, something like this is possible, though too slow to be practical:

( SELECT * FROM table1 )
UNION
( SELECT * FROM table2 )
ORDER BY RAND()
LIMIT 30 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文