Solved

how do I fix this MySQL query employing user variables?

Posted on 2011-02-26
4
424 Views
Last Modified: 2012-05-11
I have a table: products (product_id, shop_id, date)

For each shop_id, the following query retrieves the 10 rows with the most recent dates

set @num := 0, @shop_id := 0;

select product_id, shop_id, date
from (
   select product_id, shop_id, date,
      @num := if(@shop_id = shop_id, @num + 1, 1) as row_number,
      @shop_id := shop_id as dummy
  from products
  order by shop_id, date DESC
) as x where x.row_number <= 10;

Open in new window


In addition to the date and shop_id, I would also like to select the product_id for each row. However while the above query executes in about 0.35 secs on 80k of dummy data, and adding product_id to the subquery:

set @num := 0, @shop_id := NULL;# MySQL returned an empty result set (i.e. zero rows).
# MySQL returned an empty result set (i.e. zero rows).
# MySQL returned an empty result set (i.e. zero rows).
# MySQL returned an empty result set (i.e. zero rows).

select shop_id, date
from (
   select product_id, shop_id, date,
      @num := if(@shop_id = shop_id, @num + 1, 1) as row_number,
      @shop_id := shop_id as dummy
  from products
  order by shop_id, date DESC
) as x where row_number <= 10;

Open in new window


works just as well, adding product_id to the main query:

set @num := 0, @shop_id := NULL;

select product_id, shop_id, date
from (
   select product_id, shop_id, date,
      @num := if(@shop_id = shop_id, @num + 1, 1) as row_number,
      @shop_id := shop_id as dummy
  from products
  order by shop_id, date DESC
) as x where row_number <= 10;

Open in new window


causes MySQL to hang indefinitely. I am wondering what I am doing wrong.
0
Comment
Question by:bitt3n
  • 3
4 Comments
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34988911
It's having to calculate all results and then just give you the top 10. Instead just add limit 10 to the en of the query and get rid of the row numbers.
0
 

Author Comment

by:bitt3n
ID: 34988924
I'm getting the top 10 results for each shop_id, not limiting the query to 10 results.
0
 

Accepted Solution

by:
bitt3n earned 0 total points
ID: 34989391
I am closing and reposting this question because I believe it can be stated more clearly that I in my original post.
0
 

Author Closing Comment

by:bitt3n
ID: 35025216
closing to rephrase
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now