Help getting, or displaying, random records in ColdFusion from a MySQL query?

流过昼夜 提交于 2019-12-25 06:39:03

问题


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.


回答1:


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.




回答2:


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.




回答3:


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 


来源:https://stackoverflow.com/questions/4016505/help-getting-or-displaying-random-records-in-coldfusion-from-a-mysql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!