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 8628471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:33:09+00:00 2026-06-12T08:33:09+00:00

I have the following database schema: https://dl.dropbox.com/u/37915176/schema.PNG I have more than 2 millions records

  • 0

I have the following database schema:
https://dl.dropbox.com/u/37915176/schema.PNG

I have more than 2 millions records in meter_relevation. The data is coming from several electrical meters which are associated to appliances (devices) with the meter_history table.

I try to get the data of a device for a specific date range:

SELECT MR.* 
FROM device AS D, meter_history AS MH, meter AS M, meter_relevation AS MR
WHERE D.Id=MH.Id_Device 
   AND MH.Id_Meter=M.Id 
   AND M.Id=MR.Id_Meter 
   AND D.Id="8" 
   AND MR.Date>="2012-10-04" 
   AND MR.Date<="2012-10-04"

But the performance are very slow, I can get 10 seconds even if there are no record for the specified date range.

I tried EXPLAIN and I clearly see that my query is not optimal, on the meter_relevation table is listing the total number of rows, more than 2 millions: https://dl.dropbox.com/u/37915176/explain.png

Any suggestions? There is a better way? Of course I could do some work on the client side and split in several queries. But I would like to know if there are better way for a single SELECT query.

  • 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-06-12T08:33:11+00:00Added an answer on June 12, 2026 at 8:33 am

    If that is the query you’re running, it looks sub-optimal. You don’t really need the D table, or do you? Seeing as how the condition on device could be satisfied by MH.Id_device = "8".

    But supposing there are other fields which aren’t shown, then let’s rewrite:

    SELECT MR.*
        FROM meter_relevation AS MR
        JOIN meter AS M ON ( M.Id = MR.ID_Meter )
        JOIN meter_history AS MH ON ( MH.Id_Meter = M.Id )
        JOIN device AS D ON ( D.Id=MH.Id_Device AND D.Id = "8" )
    WHERE 
        MR.Date BETWEEN "2012-10-04" AND "2012-10-04";
    

    So we need indexes. The first is the most important

    CREATE INDEX mr_ndx     ON meter_relevation ( Date, Id_Meter );
    

    But try also deleting the index above and using instead:

    CREATE INDEX mr_ndx     ON meter_relevation ( Id_Meter, Date );
    
    
    CREATE INDEX m_ndx      ON meter(Id); -- This probably already exists
    CREATE INDEX mh_ndx     ON meter_history( Id_Device, Id_Meter );
    CREATE INDEX d_ndx      ON device (Id); -- This too probably already exists
    

    The above, if written just like that, is equivalent (but MySQL should realize this, so I don’t think it’s slowing you too much) to

    SELECT MR.*
        FROM meter_relevation AS MR
        JOIN meter AS M ON ( MR.ID_Meter = M.Id)
        JOIN meter_history AS MH ON (MH.Id_Device = "8" AND MH.Id_Meter = M.Id)
    WHERE 
        MR.Date BETWEEN "2012-10-04" AND "2012-10-04";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following database schema: http://lh4.ggpht.com/_SDci0Pf3tzU/SdM3XnAmmxI/AAAAAAAAEps/Ie3xW3ZVNfQ/s400/styleerror.png And this is my mapping file: <?xml
I have no control over database schema and have the following (simplified) table structure:
I have a database schema that is similar to the following: | User |
I have the following schema: Database: test. Table: per_login_user, Field: username (PK), password Database:
I have written the following code snippet to read data from the database :
I have the following database schema: table courses: id tutor_id title table course_categories: id
I have the following database schema (Microsoft SQL Server Compact Edition): How can I
I have following database schema for tabel tblpers Field Type Null Key Default Extra
I have the following database relational schema that purports to model an EMP supertype
I have a MySQL innodb database at 1.9GB, showed by following command. SELECT table_schema

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.