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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:40:49+00:00 2026-05-24T17:40:49+00:00

I’m having real difficulties optimising a MySQL query. I have to use the existing

  • 0

I’m having real difficulties optimising a MySQL query. I have to use the existing database structure, but I am getting an extremely slow response under certain circumstances.

My query is:

SELECT 
    `t`.*,
    `p`.`trp_name`,
    `p`.`trp_lname`,
    `trv`.`trv_prosceslevel`,
    `trv`.`trv_id`,
    `v`.`visa_destcountry`,
    `track`.`track_id`,
    `track`.`track_datetoembassy`,
    `track`.`track_expectedreturn`,
    `track`.`track_status`,
    `track`.`track_comments`
FROM
    (SELECT 
        *
    FROM
        `_transactions`
    WHERE
        DATE(`tr_datecreated`) BETWEEN DATE('2011-07-01 00:00:00') AND DATE('2011-08-01 23:59:59')) `t`
        JOIN
    `_trpeople` `p` ON `t`.`tr_id` = `p`.`trp_trid` AND `p`.`trp_name` = 'Joe' AND `p`.`trp_lname` = 'Bloggs'
        JOIN
    `_trvisas` `trv` ON `t`.`tr_id` = `trv`.`trv_trid`
        JOIN
    `_visas` `v` ON `trv`.`trv_visaid` = `v`.`visa_code`
        JOIN
    `_trtracking` `track` ON `track`.`track_trid` = `t`.`tr_id` AND `p`.`trp_id` = `track`.`track_trpid` AND `trv`.`trv_id` = `track`.`track_trvid` AND `track`.`track_status` IN ('New','Missing_Info',
        'En_Route',
        'Ready_Pickup',
        'Received',
        'Awaiting_Voucher',
        'Sent_Client',
        'Closed')
ORDER BY `tr_id` DESC

The results of an explain statement on the above is:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   PRIMARY     <derived2>  ALL     NULL    NULL    NULL    NULL    164     Using temporary; Using filesort

1   PRIMARY     track   ALL     status_index    NULL    NULL    NULL    4677    Using where

1   PRIMARY     p   eq_ref  PRIMARY     PRIMARY     4   db.track.track_trpid    1   Using where

1   PRIMARY     trv     eq_ref  PRIMARY     PRIMARY     4   db.track.track_trvid    1   Using where

1   PRIMARY     v   eq_ref  visa_code   visa_code   4   db.trv.trv_visaid   1   

2   DERIVED     _transactions   ALL     NULL    NULL    NULL    NULL    4276    Using where

The query times are acceptable until the value of ‘Closed’ is included in the very last track.track_status IN clause. The length of time is then increased about 10 to 15 times the other queries.

This makes sense as the ‘Closed’ status refers to all the clients whose transactions have been dealt with, wihich corresponds to about 90% to 95% of the database.

The issue is, is that in some cases, the search is taking about 45 seconds which is rediculous. I’m sure MySQL can do much better than that and it’s just my query at fault, even if the tables do have 4000 rows, but I can’t work out how to optimise this statement.

I’d be grateful for some advice about where I’m going wrong and how I should be implementing this query to produce a faster result.

Many thanks

  • 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-24T17:40:50+00:00Added an answer on May 24, 2026 at 5:40 pm

    Try this:

    SELECT t.*, 
    p.trp_name, 
    p.trp_lname, 
    trv.trv_prosceslevel, 
    trv.trv_id,
    v.visa_destcountry, 
    track.track_id, 
    track.track_datetoembassy,
    track.track_expectedreturn, 
    track.track_status, 
    track.track_comments 
    FROM 
    
    _transactions t
    JOIN _trpeople p ON t.tr_id = p.trp_trid
    JOIN _trvisas trv ON t.tr_id = trv.trv_trid 
    JOIN _visas v ON trv.trv_visaid = v.visa_code 
    JOIN _trtracking track ON track.track_trid = t.tr_id 
    AND p.trp_id = track.track_trpid 
    AND trv.trv_id = track.track_trvid 
    WHERE DATE(t.tr_datecreated) 
        BETWEEN DATE('2011-07-01 00:00:00') AND DATE('2011-08-01 23:59:59')
    
        AND track.track_status IN ('New','Missing_Info','En_Route','Ready_Pickup','Received','Awaiting_Voucher','Sent_Client', 'Closed') 
        AND p.trp_name = 'Joe' AND p.trp_lname = 'Bloggs' 
    ORDER BY tr_id DESC 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.