How do I add a photo to my comment or blog entry?
How to efficiently get a random set of records from a DB
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > ASP web-programming > How to efficiently get a random set of records from a DB 3 October 2008 23:51:49

  Recent blog posts: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

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

Laphan 3 October 2008 23:51:49
 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


Add comment
Bob Barrows 21 September 2008 14:04:37 permanent link ]
 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.c­om/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"


Add comment
Captain Paralytic 22 September 2008 14:59:06 permanent link ]
 On 21 Sep, 11:48, "Laphan" <ple...@dont.spam.c­om> 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 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.mysq­l.
Even better, just search its archives, you will find the answer to
this question, there.
Add comment
Ted Dawson 25 September 2008 05:04:15 permanent link ]
 
"Laphan" <please@dont.spam.c­om> wrote in message
news:eHH4Xe9GJHA.49­56@TK2MSFTNGP06.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

Add comment
Ken 3 October 2008 23:51:49 permanent link ]
 This works for me MySQL ASP

SQLString = "SELECT * FROM table ORDER BY RAND() LIMIT 10;"

Set rs_products= Server.CreateObject­("ADODB.Recordset")
rs_products.Open SQLString,dbConnED,­adLockReadOnly,adOpe­nDynamic

Gets 10 random products from the table - cheers


On Sep 21, 4:48am, "Laphan" <ple...@dont.spam.c­om> 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 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

Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > ASP web-programming > How to efficiently get a random set of records from a DB 3 October 2008 23:51:49

see also:
httpd-2.0.43 ExtFilterDefine
question
User cannot occur within…
пройди тесты:
see also:
i am configuring SIB in was 6.0.i i...
Hello!
PS3 processor to migrate to mobiles

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .