Solved

how do I fix this MySQL query employing user variables?

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikipedia.org/wiki/PHP  Very powerful.  But a…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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