473,698 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optimization question (indexes and joins)

I am building a query to select contacts and filter by category. I was
wondering if anyone could explain why the first select statement takes 950
times as long to execute compared to the second. I'm sure it has something
to do with the way the join is processed but I'm still curious what the
engine actually does in each case. In my test the first statement executed
in 0.03 seconds while the second took

Some additional info- In the tests there were 3911 contact records and 17164
contact_categor y records. The following fields have BTREE indexes:
contact.id, UNIQUE(contact_ category.contac t_id,
contact_categor y.category_id)

SELECT DISTINCT contacts.id FROM contacts
LEFT JOIN contact_categor y ON contacts.id=con tact_category.c ontact_id
WHERE category_id IN (1,2,3,4)
LIMIT 0,500;

----------------------------------------------------------------------------------------

SELECT DISTINCT contacts.id FROM contacts, contact_categor y
WHERE contacts.id=con tact_category.c ontact_id AND category_id IN (1,2,3,4)
LIMIT 0,500;

----------------------------------------------------------------------------------------

Any tips or clarification on the queries or suggestions how to improve them
or the indexes would be greatly appreciated.

Alex
Jul 23 '05 #1
3 1459
The primary difference is the type of joins being performed. The first
query is (obviously) a left join, using indexed columns from each table
as the join condition. The second, slower, query is an inner join,
which, according to the docs:

INNER JOIN and , (comma) are semantically equivalent in the absence of
a join condition: both produce a Cartesian product between the specified
tables (that is, each and every row in the first table are joined to all
rows in the second table).

which is then (presumably) joining on some non-indexed columns, and,
save the limit clause, would return 67,000,000+ records as opposed to
3911 in the first query.

An EXPLAIN SELECT is very helpful in determining where index and query
optimization should take place.

Alex Glass wrote:
I am building a query to select contacts and filter by category. I was
wondering if anyone could explain why the first select statement takes 950
times as long to execute compared to the second. I'm sure it has something
to do with the way the join is processed but I'm still curious what the
engine actually does in each case. In my test the first statement executed
in 0.03 seconds while the second took

Some additional info- In the tests there were 3911 contact records and 17164
contact_categor y records. The following fields have BTREE indexes:
contact.id, UNIQUE(contact_ category.contac t_id,
contact_categor y.category_id)

SELECT DISTINCT contacts.id FROM contacts
LEFT JOIN contact_categor y ON contacts.id=con tact_category.c ontact_id
WHERE category_id IN (1,2,3,4)
LIMIT 0,500;

----------------------------------------------------------------------------------------

SELECT DISTINCT contacts.id FROM contacts, contact_categor y
WHERE contacts.id=con tact_category.c ontact_id AND category_id IN (1,2,3,4)
LIMIT 0,500;

----------------------------------------------------------------------------------------

Any tips or clarification on the queries or suggestions how to improve them
or the indexes would be greatly appreciated.

Alex

Jul 23 '05 #2
Thanks for the reply, but if you re-read what I wrote the bizarre thing that
is happening is that the INNER JOIN (Cartesian product) is faster that the
LEFT JOIN which is what I don't quite understand. It seems counter-intuitive
that this be the case...

"hoonew" <ho****@ino.now > wrote in message
news:tlzEe.1169 31$gc6.54828@ok epread04...
The primary difference is the type of joins being performed. The first
query is (obviously) a left join, using indexed columns from each table as
the join condition. The second, slower, query is an inner join, which,
according to the docs:

INNER JOIN and , (comma) are semantically equivalent in the absence of a
join condition: both produce a Cartesian product between the specified
tables (that is, each and every row in the first table are joined to all
rows in the second table).

which is then (presumably) joining on some non-indexed columns, and, save
the limit clause, would return 67,000,000+ records as opposed to 3911 in
the first query.

An EXPLAIN SELECT is very helpful in determining where index and query
optimization should take place.

Alex Glass wrote:
I am building a query to select contacts and filter by category. I was
wondering if anyone could explain why the first select statement takes
950 times as long to execute compared to the second. I'm sure it has
something to do with the way the join is processed but I'm still curious
what the engine actually does in each case. In my test the first
statement executed in 0.03 seconds while the second took

Some additional info- In the tests there were 3911 contact records and
17164 contact_categor y records. The following fields have BTREE indexes:
contact.id, UNIQUE(contact_ category.contac t_id,
contact_categor y.category_id)

SELECT DISTINCT contacts.id FROM contacts
LEFT JOIN contact_categor y ON contacts.id=con tact_category.c ontact_id
WHERE category_id IN (1,2,3,4)
LIMIT 0,500;

----------------------------------------------------------------------------------------

SELECT DISTINCT contacts.id FROM contacts, contact_categor y
WHERE contacts.id=con tact_category.c ontact_id AND category_id IN
(1,2,3,4)
LIMIT 0,500;

----------------------------------------------------------------------------------------

Any tips or clarification on the queries or suggestions how to improve
them or the indexes would be greatly appreciated.

Alex

Jul 24 '05 #3
Indeed, I need to read more carefully... What does EXPLAIN SELECT return?

Alex Glass wrote:
Thanks for the reply, but if you re-read what I wrote the bizarre thing that
is happening is that the INNER JOIN (Cartesian product) is faster that the
LEFT JOIN which is what I don't quite understand. It seems counter-intuitive
that this be the case...

Jul 24 '05 #4

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

Similar topics

0
1719
by: Jesse Sheidlower | last post by:
After some discussion in a separate thread, I've been trying to get a better understanding of the workings of multiple-column indexes, and think I'm still missing the point. I understand indexing (last_name, first_name) in that order if you'd always be searching last names and only be searching first names in combination. What I'm trying to understand is how you would set up these indexes when you'd always be doing joins with another...
2
1704
by: ensnare | last post by:
This query is running REAL slow ... like 1.2 secs ... any ideas on how I could optimize it? Perhaps my indexes are incorrect? $this->query = "SELECT m.username as username, e.title as title, e.exhibition_id as exhibition_id, LEFT(e.text,50) as text, e.random_key,
3
3347
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four inner joins, as follows : SELECT DISTINCT upcards.id,statuskey.status,upcards.firstname,upcards.lastname,originkey.ori gin,associatekey.username,associatekey2.username,upcards.deleted FROM upcards,status,origins,associates INNER JOIN status...
5
1359
by: Kenneth Courville | last post by:
I have a couple of tables that look like this (not excactly but close enough): id int fname varchar(50) lname varchar(50) id int
19
2085
by: nospammmer | last post by:
Hello group, I have a rather general but interesting inquiry that is related to PHP and I hope this is the appropriate place to post it. I'm looking for a way to improve dramatically the performance of my PHP application. The application is getting slow as it is taking more load. It is performing a very high number of queries to a database, and I believe that this is taking up most of the ressources.
10
8291
by: Troels Arvin | last post by:
Hello, At http://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems it's stated that DB2 doesn't have bitmap indexes. This seemed strange to me, so I tried looking in the DB2 UDB (for LUW) manual without luck. Googling turned up vector indexes, but it seems that those are only available for the mainframe and AS/400 versions of UDB. Can someone summarize UDB's support for indexes suitable for working with
22
1767
by: Zamdrist | last post by:
I am tasked with maintaining a large database still on SQL Server 7.0. Performance is an issue not surprisingly and I've targeted two rather large tables to see what I can do with the indexes. The 2 tables are described as follows: MatterConflicts: Fields: MatterConflicts varchar(16), Matters varchar(16), HitMatters varchar(16), IsInclude varchar(1) Index: MatterConflicts
1
1431
by: annroberti | last post by:
I have web page that users use to specify sort and selection critieria for a query. So there is a "most common" type set of where clauses, but they can change. Every time the query is run, there are where clauses on multiple columns, and joins from some columns to other tables, and then it is sorted by a different column. I am wondering if I should have a clustered index on this table since there are so many columns used in where clauses....
1
3038
by: Don Li | last post by:
Hi, Env: MS SQL Server 2000 DB Info (sorry no DDL nor sample data): tblA has 147249 rows -- clustered index on pk (one key of datatype(int)) and has two clumns, both are being used in joins; intersecTbl4AB has 207016 rows -- clustered index on two fks and
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.