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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:05:57+00:00 2026-05-24T21:05:57+00:00

What is generally considered the most efficient way to do this type of query?

  • 0

What is generally considered the most efficient way to do this type of query?

We have a database of 10 years worth of laboratory data and we would like to select out performance data for various tests. This query for example will select the number of hours its taken to do a test and calculate an average turnaround time and allow us to plot a sparkline of avg TAT per day.

Say we have 100 test names is it acceptable in terms of performance to iterate over the test names in a loop and fire this query off once per loop? Or is there a more efficient way?

SELECT 
  Date_Authorised_Index.Date_Authorised
  , Result_Set.Date_Booked_In
  , avg(DATEDIFF('hh',Result_Set.Date_Time_Booked_In,Result_Set.Date_Time_Authorised)) as HrsIn
  , count(Date_Authorised_Index.Date_Authorised) as numbers
  , Date_Authorised_Index.Registration_Number
  , Date_Authorised_Index.Request_Row_ID
  , Date_Authorised_Index.Specimen_Number
  , Result_Set.Authorised_By
  , Result_Set.Namespace
  , Result_Set.Set_Code
  , Result_Set.Date_Time_Authorised
  , Request.Date_Time_Received
  , Request.Location 
FROM 
  Date_Authorised_Index Date_Authorised_Index
  , Result_Set Result_Set
  , Request 
WHERE 
  Date_Authorised_Index.Date_Authorised = Result_Set.Date_Authorised 
  AND Date_Authorised_Index.Request_Row_ID = Request.Request_Row_ID
  AND Date_Authorised_Index.Request_Row_ID = Result_Set.Request_Row_ID 
  AND (Date_Authorised_Index.Discipline='C') AND Result_Set.Set_Code=? 
GROUP BY 
  Result_Set.Date_Booked_In
  • 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-24T21:05:58+00:00Added an answer on May 24, 2026 at 9:05 pm

    For starters I would rewrite this query so it uses explicit join syntax.
    Also even though MySQL does not force you to restate every non-aggregate column in the group by clause that doesn’t mean that’s a good thing.
    Unless the Result_Set.Date_Booked_In uniquely identifies a row, you are selecting random values from a multiple of rows.

    SELECT 
      dai.Date_Authorised
      , rs.Date_Booked_In
      , avg(DATEDIFF('hh',rs.Date_Time_Booked_In,rs.Date_Time_Authorised)) as HrsIn
      , count(dai.Date_Authorised) as numbers
      , dai.Registration_Number
      , dai.Request_Row_ID
      , dai.Specimen_Number
      , rs.Authorised_By
      , rs.Namespace
      , rs.Set_Code
      , rs.Date_Time_Authorised
      , r.Date_Time_Received
      , r.Location 
    FROM 
      Date_Authorised_Index dai    
    INNER JOIN Result_Set rs ON (dai.Date_Authorised = rs.Date_Authorised
                             AND dai.Request_Row_ID = rs.Request_Row_ID)
    INNER JOIN Request R ON (dai.Request_Row_ID = r.Request_Row_ID)
    WHERE 
      (dai.Discipline= 'C') AND rs.Set_Code=? 
    GROUP BY 
      rs.Date_Booked_In
    

    If you want to select a 100 rows in one go, just make a new table with the set_codes you want to select and join against that.
    Make sure you index the field sc.set_code (or better yet make it the primary key)

    SELECT lots_of_columns
    FROM table1 as dai
    INNER JOIN table2 as rs ON (what you joined on before)
    INNER JOIN table3 as r ON (same here)
    INNER JOIN Setcodes as sc ON (sc.Set_code = rs.SetCode)  <<-- extra join.
    WHERE 
      dai.discipline = 'C'
    GROUP BY  rs.Date_Booked_In
    

    Or you can even use a `IN (…) like below, although that will propably be slower than a join.

    SELECT lots_of_columns
    FROM table1 as dai
    INNER JOIN table2 as rs ON (what you joined on before)
    INNER JOIN table3 as r ON (same here)
    WHERE 
      dai.discipline = 'C' AND rs.Set_Code IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
    GROUP BY  rs.Date_Booked_In
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Generally the database server is the biggest, most expensive box we have to buy
Generally I connect and retrieve data using the standard way (error checking removed for
Is it generally considered bad practice to provide Iterator implementations that are infinite; i.e.
Generally, how bad of a performance hit is storing a file in a database
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
Generally speaking, the SQL queries that I write return unformatted data and I leave
I'm wondering if writing functions like this is considered good or bad form. def
I have posted this to ServerFault, but the Node.js community seems tiny there, so
Suppose I have a few lines out of wikipedia XML that looks like this:
I understand that C++ is generally considered better than Java for games (at least

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.