473,473 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Composite index

1 New Member
I am having the following query and I am wondering how should I construct an index for each of the clients and products tables.

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.      clients.id AS id, 
  3.      clients.name AS name, 
  4.      COUNT(DISTINCT products.product_id) AS counts 
  5. FROM 
  6.      clients 
  7. INNER JOIN 
  8.      products ON (clients.id = products.client_id) 
  9. WHERE 
  10.      CAST("2008-07-30" AS DATE) BETWEEN products.start AND products.end AND 
  11.      products.flag=0 AND 
  12.      products.price<=products.value AND 
  13.      products.price<=clients.balance 
  14. GROUP BY 
  15.      clients.id 
  16. ORDER BY 
  17.      clients.name ASC 
  18. LIMIT 0, 100
  19.  
Also, with all these Where, Inner join, Group by, Order by clauses, what order does a composite index apply to a query similar to the one above?
Jul 30 '08 #1
3 2182
coolsti
310 Contributor
I am having the following query and I am wondering how should I construct an index for each of the clients and products tables.

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.      clients.id AS id, 
  3.      clients.name AS name, 
  4.      COUNT(DISTINCT products.product_id) AS counts 
  5. FROM 
  6.      clients 
  7. INNER JOIN 
  8.      products ON (clients.id = products.client_id) 
  9. WHERE 
  10.      CAST("2008-07-30" AS DATE) BETWEEN products.start AND products.end AND 
  11.      products.flag=0 AND 
  12.      products.price<=products.value AND 
  13.      products.price<=clients.balance 
  14. GROUP BY 
  15.      clients.id 
  16. ORDER BY 
  17.      clients.name ASC 
  18. LIMIT 0, 100
  19.  
Also, with all these Where, Inner join, Group by, Order by clauses, what order does a composite index apply to a query similar to the one above?
Try to run your query while prepending the word "Explain" to it. This will give you a table of information, that may help you decide what indexes will help best with our query. I assume that you have the products.product_id and clients.id as primary keys to their respective tables. If so, then these two are already indexed.

Basically, indexes help best for items that appear in where clauses, and group by clauses. So it might help to add an index on products.client_id. It may also help on some of the entities that appear in your where clause.

But I would use the EXPLAIN syntax to help me here, by adding indexes, seeing how that affects the EXPLAIN results, then maybe changing the indexes.
Jul 31 '08 #2
akashazad
38 New Member
Try to run your query while prepending the word "Explain" to it. This will give you a table of information, that may help you decide what indexes will help best with our query. I assume that you have the products.product_id and clients.id as primary keys to their respective tables. If so, then these two are already indexed.

Basically, indexes help best for items that appear in where clauses, and group by clauses. So it might help to add an index on products.client_id. It may also help on some of the entities that appear in your where clause.

But I would use the EXPLAIN syntax to help me here, by adding indexes, seeing how that affects the EXPLAIN results, then maybe changing the indexes.



Dude I just didn't got your "Explain" method can u just give it
with some example
Jul 31 '08 #3
coolsti
310 Contributor
There is nothing really to Explain with the Explain. You just put it at the beginning of the query, and mysql then does not run the query, but gives you information about how many rows per table need to be examined. And which indexes maybe are used in the query.

It is up to you, then, to decide where adding extra indexes will help. At the worst case, you have a query where, let us say 5, tables are joined, and the Explain syntax tells you that ALL the rows of each table need to be examined. Expect then a long query time. At the best case, only one table at most needs all of its rows examined, maybe none of them do. This then will execute quickly.

I am sorry I have no time to give you examples. I explain to you what is in my head, but I do not have the time to sit at a console window and create something for you to see. Please refer to the Mysql documentation on the Explain keyword.
Jul 31 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike Coppinger | last post by:
I have a problem that has raised a couple of questions. SITUATION: I have a table called pr_persona that has a composite key comprising pr_persona_db CHAR(2) pr_persona_id INT(11) auto...
10
by: deko | last post by:
I understand it's possible to make a composite Primary Key by holding down the control key and selecting multiple fields, then right-clicking and selecting Primary Key. But I've heard that's not a...
18
by: Thomas A. Anderson | last post by:
I am a bit confused in creating a composite primary key. I have three table with two of the tables containing primary keys. I have two of the tables (each with a primary key) having one to many...
0
by: Satya Bojanapally | last post by:
Hi, I am unable to add a pager for this composite control. I had created a composite control in C#. The control is having 5 labels, one radio button and one DropDownList control. The composite...
2
by: Peter Lindquist | last post by:
I had a fun issue this morning, and now I'm wondering if I violated a rule I didn't know about. Any insight would be much appreciated. All operations involving this particular table may be assumed...
2
by: bobby_b_ | last post by:
I have a table where fields 1 and 2 make up the primary key. Because of this, I have a unique composite index on fields 1 and 2 (as required by DB2). Now my question is: Fields 1 and 2 are also...
13
by: Baihao Yuan | last post by:
Hi, I created a composite index (lastname, firstname). I know the following queries will use this index: WHERE lastname = ... WHERE lastname = ... AND firstname = ... Also this won't use...
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
0
by: lior3790 | last post by:
Hello to all the smart people, I'm trying to create a composite control which include nested controls from the great Ajax Dev. I overcame a lot of issues and can be helpful in some but my...
2
by: lfhenry | last post by:
Hi All, I am about to make some changes to a process we have. The new process will have a new table who's job it is to store temporarily data such as customer, clerkno,productno, storeno , amount,...
0
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,...
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...
0
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.