Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5982959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:09:43+00:00 2026-05-22T22:09:43+00:00

I have a product order table in mysql. It’s like this: create table `order`

  • 0

I have a product order table in mysql. It’s like this:

create table `order`
  (productcode int,
   quantity tinyint,
   order_date timestamp,
   blablabla)

then, to get rate of rise, i wrote this query:

SELECT thismonth.productcode,
       (thismonth.ordercount-lastmonth.ordercount)/lastmonth.ordercount as riserate
  FROM ( (SELECT productcode,
                 sum(quantity) as ordercount
            FROM `order`
            where date_format(order_date,'%m') = 12
            group by productcode) as thismonth,
          (SELECT productcode,
                  sum(quantity) as ordercount
             FROM `order`
             where date_format(order_date,'%m') = 11
             group by productcode) as lastmonth)
WHERE thismonth.productcode = lastmonth.productcode
ORDER BY riserate;

but it runs about 30s on my pc(200000 records, 200MB(include other fields)).
Are there any way to increase query speed? I already create index for productcode field.

I thought the reason of low performance is ‘GROUP BY’, is there any different way?

I tried your answers, but all of them seems not work, and I was wondering if there is something wrong with index(it’s not me who created them), so I delete all index and re-created them, everything goes fine — It only takes 3-4s. And difference between my query and yours is not very obvious. But REALLY thanks you guys, I learned a lot 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:09:44+00:00Added an answer on May 22, 2026 at 10:09 pm

    Try adding an index on (ORDER_DATE, PRODUCTCODE) and change the query to eliminate the use of the DATE_FORMAT function, as in:

    SELECT thismonth.productcode,
           (thismonth.ordercount-lastmonth.ordercount)/lastmonth.ordercount as riserate   
      FROM ( (SELECT productcode,
                     sum(quantity) as ordercount
                FROM `order`
                WHERE ORDER_DATE BETWEEN '01-12-2010' AND '31-12-2010'
                      GROUP BY PRODUCTCODE) as thismonth,
             (SELECT productcode,
                     sum(quantity) as ordercount
                FROM `order`
                WHERE ORDER_DATE BETWEEN '01-11-2010' AND '30-11-2010'
                group by productcode) as lastmonth)
      WHERE thismonth.productcode = lastmonth.productcode
      ORDER BY riserate;
    

    Share and enjoy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 10M-row table product with fields like color (int), price (float), weight
Let's say we have a Product table, and Order table and a (junction table)
I have a table like this table name: products +----+-------------+-----------+ | id | name
I have a MySQL table which contains the following type of information: Date product
I'm building an e-commerce website. I have a Product and Order models. It's possible
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
I have a basic Customer/Order/OrderItem/Product object graph. Customer has Many Orders, Order has Many
I have a order list and I want to generate and rank the product
Hi i'm struggling to write a particular MySQL Join Query. I have a table
I have an order table in my database and once a user logs into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.