473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Optimizing a query that uses a left join

Hi, I've spent hours trying to optimize this simple query:

SELECT count(sites_jobs.id) as jobCount,
sites_jobs_categories.frn_site_id, sites_jobs_categories.id,
sites_jobs_categories.name,
FROM sites_jobs_categories
LEFT JOIN sites_jobs ON sites_jobs.frn_jobs_categories_id =
sites_jobs_categories.id
GROUP BY sites_jobs_categories.id
HAVING sites_jobs_categories.frn_site_id = #request.siteId#
ORDER BY sites_jobs_categories.name ASC

I placed an index on sites_jobs_.frn_jobs_categories_id, which gave me
a nice speed boost, but for some reason mysql won't use the index on
sites_jobs_categories.frn_site_id (explains states that there are no
'possible keys'.)

Any help or pointer would be much appreciated!

May 7 '06 #1
1 1781
ma**********@googlemail.com wrote:
Hi, I've spent hours trying to optimize this simple query:

SELECT count(sites_jobs.id) as jobCount,
sites_jobs_categories.frn_site_id, sites_jobs_categories.id,
sites_jobs_categories.name,
FROM sites_jobs_categories
LEFT JOIN sites_jobs ON sites_jobs.frn_jobs_categories_id =
sites_jobs_categories.id
GROUP BY sites_jobs_categories.id
HAVING sites_jobs_categories.frn_site_id = #request.siteId#
ORDER BY sites_jobs_categories.name ASC

I placed an index on sites_jobs_.frn_jobs_categories_id, which gave me
a nice speed boost, but for some reason mysql won't use the index on
sites_jobs_categories.frn_site_id (explains states that there are no
'possible keys'.)


Try putting the condition in a WHERE clause instead of a HAVING clause.
Traditionally, WHERE is for restricting rows, HAVING is for
restricting groups.

(In spite of the fact that some people put row conditions in the HAVING
clause if they need to reference a column alias defined in the
select-list, because SQL says that we can't use column aliases in the
WHERE clause.)

Anyway, it might be easier for the optimizer to use an index for the
WHERE clause than the HAVING clause.

Also keep in mind that MySQL has a limitation of using only one index
per table in a given query. It might have decided that using an index
on sites_jobs_categories.name (for the ORDER BY) is of greater overall
benefit than using an index on frn_site_id. That is, if you have an
index on that name column.

Regards,
Bill K.
May 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gord barq | last post by:
I have this query which does a left outer join and it takes forever (like half a day). Here are the results of an explain analysis. mysql> explain SELECT count(searchresult.title) AS number,...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
6
by: Steven D.Arnold | last post by:
I have a query which does not use column indexes that it should use. I have discovered some interesting behaviors of Postgres which may indicate a bug in the database's query planning. Take a...
7
by: DFS | last post by:
This UNION query is very slow. With only 3,000 records in the Parent table and 7,000 records in the Child table, it takes about 60 seconds to run and returns about 2200 records. Access 97. All...
2
by: AJ | last post by:
Hi all, I have this monster query (at least i think it is). SELECT c.ID, c.Company_Name, p., 1 As QueryNbr FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID) LEFT JOIN Package...
5
by: Merennulli | last post by:
To start with, I'll give a simplified overview of my data. BaseRecord (4mil rows, 25k in each Region) ID | Name | Region | etc OtherData (7.5mil rows, 1 or 2 per ID) ID | Type(1/2) | Data ...
2
by: erbrose | last post by:
Hello All! Hoping some folks could help me optimize and or choose the best route to do this process. First off, here is what I am trying to achieve. I have a (fairly large) table of ~34million rows...
11
by: lenygold via DBMonster.com | last post by:
Hi everybody! This query is supposed to count consecutive years from the current year without OLAP. Input Table: ID DateCol 1 02/01/2006 1 01/01/2006 1 01/01/2005
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.