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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:15:58+00:00 2026-06-07T03:15:58+00:00

I am running a query joining multiple tables through a date range search and

  • 0

I am running a query joining multiple tables through a date range search and stuck trying to figure out how to further optimize it.

SELECT ACC.name AS account_name, CAMP.account_id AS account_id,CAMP.name AS campaign_name,CAMP.id AS campaign_id,ADG.id AS adgroup_id,ADG.name AS adgroup_name,KW.text AS keyword_name,
SUM(SPENT.billed_clicks) AS billed_clicks,KW.id AS keyword_id,KW.status_id AS status_id FROM account ACC, campaign CAMP,adgroup ADG,adgroup_keyword KW INNER JOIN keyword_spent SPENT
ON KW.id = SPENT.keyword_id WHERE     summary_date >= '2012-03-01' AND summary_date <= '2012-03-04' AND KW.adgroup_id = ADG.id AND ADG.campaign_id = CAMP.id AND CAMP.account_id = ACC.id
GROUP BY keyword_id

The EXPLAIN on this yields the following –

+----+-------------+-------+--------+----------------------------+--------------+---------+---------------------------------+--------+----------------------------------------------+
| id | select_type | table | type   | possible_keys              | key          | key_len | ref                             | rows   | Extra                                        |
+----+-------------+-------+--------+----------------------------+--------------+---------+---------------------------------+--------+----------------------------------------------+
|  1 | SIMPLE      | SPENT | range  | summary_date               | summary_date | 3       | NULL                            | 752191 | Using where; Using temporary; Using filesort | 
|  1 | SIMPLE      | KW    | eq_ref | PRIMARY,FK1948D0E6ED3A5544 | PRIMARY      | 8       | clicksummarydb.SPENT.keyword_id |      1 |                                              | 
|  1 | SIMPLE      | ADG   | eq_ref | PRIMARY,FKBBC2083C29112FD0 | PRIMARY      | 8       | advertisedb.KW.adgroup_id       |      1 |                                              | 
|  1 | SIMPLE      | CAMP  | eq_ref | PRIMARY,FKF7A90110246F33C4 | PRIMARY      | 8       | advertisedb.ADG.campaign_id     |      1 |                                              | 
|  1 | SIMPLE      | ACC   | eq_ref | PRIMARY                    | PRIMARY      | 8       | advertisedb.CAMP.account_id     |      1 |                                              | 
+----+-------------+-------+--------+----------------------------+--------------+---------+---------------------------------+--------+----------------------------------------------+

The keyword_spent table contains more than 1.5 million rows and here is the show create table on it

 | keyword_spent | CREATE TABLE `keyword_spent` (
   `id` bigint(20) NOT NULL auto_increment,
   `summary_date` date NOT NULL,
   `adgroup_id` bigint(20) NOT NULL,
   `keyword_id` bigint(20) NOT NULL,
   `billed_clicks` int(11) default NULL,
   `un_billed_clicks` int(11) default NULL,
   `spent` decimal(20,5) default NULL,
   `last_click_recno` bigint(20) default NULL,
   `campaign_id` bigint(20) NOT NULL,
   `account_id` bigint(20) NOT NULL,
   `total_convs` bigint(20) unsigned default '0',
    PRIMARY KEY  (`id`),
   UNIQUE KEY `keyword_spent_uniq` (`summary_date`,`adgroup_id`,`keyword_id`),
   KEY `idx_account_id` (`account_id`),
   KEY `idx_kw_id` (`keyword_id`),
   KEY `adgroup_id` (`adgroup_id`),
   KEY `campaign_id` (`campaign_id`),
   KEY `summary_date` (`summary_date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 | 

I don’t understand why near 750,000 rows are being scanned when there are no more than 100,000 records in that date range.

Also, why is it doing a filesort instead of using indexes. ?

  • 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-07T03:16:00+00:00Added an answer on June 7, 2026 at 3:16 am

    File sorts aren’t necessarily bad. As shown in Baron Schwartz’s blog post, file sorts aren’t necessarily about files. It’s just a quick sort that is used when there are no valid indexes available.

    As an idea for how to optimize: perhaps have all of the aggregate data be in its own subquery, and join that data? I’m thinking something like this (make adjustments as needed):

    SELECT ACC.name AS account_name,
    CAMP.account_id AS account_id,
    CAMP.name AS campaign_name,
    CAMP.id AS campaign_id,
    ADG.id AS adgroup_id,
    ADG.name AS adgroup_name,
    KW.text AS keyword_name,
    KW.id AS keyword_id,
    JOINED.billed_clicks AS billed_clicks,
    JOINED.un_billed_clicks AS un_billed_clicks,
    JOINED.total_clicks AS total_clicks,
    JOINED.spent AS spent,
    JOINED.total_convs AS total_convs
    FROM account ACC
    INNER JOIN campaign CAMP ON ACC.id = CAMP.account_id
    INNER JOIN adgroup ADG ON CAMP.id = ADG.campaign_id
    INNER JOIN adgroup_keyword KW ON ADG.id = KW.adgroup_id
    INNER JOIN (SELECT
        SUM(billed_clicks) AS billed_clicks,
        SUM(un_billed_clicks) AS un_billed_clicks,
        SUM(billed_clicks) + SUM(un_billed_clicks) AS total_clicks,
        SUM(spent) AS spent,
        SUM(total_convs) AS total_convs,
        id AS keyword_id
        FROM keyword_spent
        GROUP BY keyword_id
    ) JOINED ON JOINED.keyword_id = KW.id
    

    Hopefully I’m understanding this right. There is one benefit to this solution: the group by/aggregates are kept separate, and you don’t have to worry about group by-ing the other columns, which you never did in the original example.

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

Sidebar

Related Questions

I'm running a query in php/mysql that joins several tables and I'm trying to
I'm running a query that is timing out for first two times and returning
I am trying to speed up a long running query that I have (takes
I am trying to find expired session by running query on this collection {
i am wondering is that possible (best practice) to run two long running query
Running this query takes a long time: SELECT host,ip FROM arecords WHERE ip IN
Running a query which should return all open questions along with the last update
After running this query statement with my database: select * from articles where content
Im running this query on the same server as the web application, so SPQuery.ExpandRecurrence
I'm running a query which looks like this SELECT parent.field, child.field FROM parent JOIN

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.