Solved

how do I fix this MySQL query employing user variables?

Posted on 2011-02-26
4
429 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 39

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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