473,625 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to efficiently get a random set of records from a DB

Hi All

I use a MySQL DB with my ASP classic web app.

I've been asked if I can create a routine whereby I get a random number of
products (records) from the DB and display these on the site. Basically
every time a visitor hits the home page, they want the site to display a
random selection of say 6 products.

I have no problems getting the data and displaying it, my problem is
grabbing 6 random records efficiently.

My initial thought was bring back all of the records to a local array (like
I normally do) and then pick 6 random records in VBscript, but if a DB has
12,000 products this is a silly server hit to bring them all back just to
display 6 out of the 12,000.

The products table doesn't have an auto-number field, the unique-ness is
managed by the product's own unique stock code, but this isn't a sequential
number - its alphanum.

Any ideas on how I can get 6 randoms from the DB rather than bringing all
locally?

Thanks
Sep 21 '08 #1
3 2186
Laphan wrote:
Hi All

I use a MySQL DB with my ASP classic web app.

I've been asked if I can create a routine whereby I get a random
number of products (records) from the DB and display these on the
site. Basically every time a visitor hits the home page, they want
the site to display a random selection of say 6 products.
I have nothing to add to this:
http://www.aspfaq.com/show.asp?id=2132
.... especially since I'm not familiar with the SQL dialect used by MySQL

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Sep 21 '08 #2
On 21 Sep, 11:48, "Laphan" <ple...@dont.sp am.comwrote:
Hi All

I use a MySQL DB with my ASP classic web app.

I've been asked if I can create a routine whereby I get a random number of
products (records) from the DB and display these on the site. *Basically
every time a visitor hits the home page, they want the site to display a
random selection of say 6 products.

I have no problems getting the data and displaying it, my problem is
grabbing 6 random records efficiently.

My initial thought was bring back all of the records to a local array (like
I normally do) and then pick 6 random records in VBscript, but if a DB has
12,000 products this is a silly server hit to bring them all back just to
display 6 out of the 12,000.

The products table doesn't have an auto-number field, the unique-ness is
managed by the product's own unique stock code, but this isn't a sequential
number - its alphanum.

Any ideas on how I can get 6 randoms from the DB rather than bringing all
locally?

Thanks
Ask this over on comp.databases. mysql.
Even better, just search its archives, you will find the answer to
this question, there.
Sep 22 '08 #3

"Laphan" <pl****@dont.sp am.comwrote in message
news:eH******** ******@TK2MSFTN GP06.phx.gbl...
Hi All

I use a MySQL DB with my ASP classic web app.

I've been asked if I can create a routine whereby I get a random number of
products (records) from the DB and display these on the site. Basically
every time a visitor hits the home page, they want the site to display a
random selection of say 6 products.

I have no problems getting the data and displaying it, my problem is
grabbing 6 random records efficiently.

My initial thought was bring back all of the records to a local array
(like
I normally do) and then pick 6 random records in VBscript, but if a DB has
12,000 products this is a silly server hit to bring them all back just to
display 6 out of the 12,000.

The products table doesn't have an auto-number field, the unique-ness is
managed by the product's own unique stock code, but this isn't a
sequential
number - its alphanum.

Any ideas on how I can get 6 randoms from the DB rather than bringing all
locally?

Thanks

Sep 25 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6281
by: Keith Griffiths | last post by:
I'm trying to do a search under a set criteria followed by a selection of random entries meeting this criteria. But I don't seem to be able to achieve this. The idea being to search on say subject and then select a random set of records meeting that subject. Any ideas or thought would be helpful. I'm using Access XP to try this out. TIA
3
2391
by: Wim Roffil | last post by:
Hi, When I do a select with a limit I get always the same records. Usually this is the desidered effect. But now I want to do a random select. So every time I do a select I should get a different set of results. What is the best way to achieve this effect?
10
2645
by: Russell Mangel | last post by:
What would be the best way to parse this XML document? I want to avoid using XMLDocument. I don't know if I should use XMLTextReader, or Xpath classes. There is only one element <MessageStore> element in the document, "always" at the end of the document. There will be thousands of <Messages> elements, "always" before <MessageStore> element. 1st Step
4
5161
by: yf | last post by:
A KB article "http://support.microsoft.com/default.aspx?scid=kb;en-us;209599" tells that the maximum number of records that a table may hold if the PRIMARY key data type is set to AUTONUMBER is 4,294,967,295. Suppose the PRIMARY key data type is set to "RANDOM" AutoNumber. Suppose an application (a) successfully INSERTS "X" records, then (b) successfully DELETES "Y" records (X >= Y), then
2
4837
by: IceCube | last post by:
Hello, I would like to select/filter at random 30 records out of an Access-table of 1500 records. I know the option "Top" which gives me the possibility to see the 30 first records of the table The problem is that this option is linked to the way of sorting the table. So you will always select from a very restraint part of the 1500 records. Is there a possibility * to select or sort at random in Access. Maybe there exists an
4
8243
by: Greg Strong | last post by:
Hello All, Is it possible to create multiple random numbers in a query where there are numerous records? I've created a custom function. When I use it in a query it creates the same random number for ALL the records. It appears the function is only getting called once, therefore only one random number is being generated for all the records. I want a different random number for each record. Is this possible, or do I have to do it all...
3
5295
by: Cybertof | last post by:
Hello, Is there a simple way to read a random access file that has been created with VB6 using direct writing to disk of Type....End Type structures ? I have not found this possibility in C#. Is it possible to map directly a buffer (read from a stream) to a Structure in memory ? Maybe a unmanaged C function call....but isn't there any overhead in call unmanaged code ?
26
3138
by: Jimmy | last post by:
ill have a database with 1 table and 3 fields: ID FIRSTNAME LASTNAME (the ID field will be the auto incrementing index) there might be 10 records in the DB, there might be 10,000. i need to open the DB and randomly select a record (and then display the name, which i dont have a problem with) how can i randomly select a record? im guessing id have to open a recordset
31
2544
by: krypto.wizard | last post by:
How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer.
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7188
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6119
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.