473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Indexes, Unions and Joins greatly appreciated!

1 New Member
Hello everyone,

I have a fair sized table now (1,955,041 rows) and it currently has two indexes:
PRIMARY is the ID number
and
Keywords is a FULLTEXT index of the Keywords column (Text).
Expand|Select|Wrap|Line Numbers
  1. +----------------------+------------------+------+-----+---------+----------------+
  2. | Field                | Type             | Null | Key | Default | Extra          |
  3. +----------------------+------------------+------+-----+---------+----------------+
  4. | ID                   | int(20) unsigned |      | PRI | NULL    | auto_increment |
  5. | Disabled             | char(2)          |      |     | 0       |                |
  6. | Keywords             | text             |      |     |         |                |
  7. | Category             | varchar(100)     |      |     |         |                |
  8. | Category2            | varchar(20)      |      |     |         |                |
  9. | Category3            | varchar(20)      |      |     |         |                |
  10. | Category4            | varchar(20)      |      |     |         |                |
  11.  
Edited for readability - ronverdonk

In my first query type I check to see it isn't Disabled and then check the Keywords:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM files WHERE Disabled='0' AND MATCH (Keywords) 
  2. AGAINST ('+tree' IN BOOLEAN MODE) ORDER by ID;
In my second query type I do not have keywords but am rather checking to see if an image belongs to a particular category like so:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM files WHERE Disabled='0' AND (Category='Food' 
  2. OR Category2='Food' OR Category3='Food' OR Category4='Food') ORDER by ID
I am very much a beginner with MySQL. My question is, are there any other indexes (or anything else) that I can do that will speed up the results, some of which are appallingly slow!?

For the first query I have a feeling self-joining the table might help but it rather makes my head spin trying to figure out how that works. (I guess I really should take a course if I have to do much more of this stuff lol!)

I was playing with the UNION command for the second query and came up with this:
Expand|Select|Wrap|Line Numbers
  1. (SELECT * FROM files WHERE Disabled='0' AND Category='Food')
  2. UNION
  3. (SELECT * FROM files WHERE Disabled='0' AND Category2='Food')
  4. UNION
  5. (SELECT * FROM files WHERE Disabled='0' AND Category3='Food')
  6. UNION
  7. (SELECT * FROM files WHERE Disabled='0' AND Category4='Food')
  8.  ORDER by ID;
If this is correct then should I also have 4 corresponding indexes?

Any help you can give me would be greatly appreciated!
Thanks,
Alison
Oct 24 '06 #1
1 1328
miller
1,089 Recognized Expert Top Contributor
First query first: Are you sure that you have an index on "keywords". Your DESC table isn't showing one. Do a "SHOW INDEX FROM files;" to make sure.

Secondly, yes, the only way to increase the speed of the second query would be to add 4 individual indexes on each of the category columns. You can try this, but I would suggest that you limit the number of characters in each index so as to prevent the size of the index and there subsequent hit on update and insert performance:

ALTER TABLE files ADD INDEX Category (Category(5));
ALTER TABLE files ADD INDEX Category2 (Category2(5));
ALTER TABLE files ADD INDEX Category3 (Category3(5));
ALTER TABLE files ADD INDEX Category4 (Category4(5));

Finally, you may not have to split up your query with UNION's. MySQL may end up doing that translation for you. But to make sure, do an EXPLAIN call on your query to verify that indexes are being utilized.

And of course, this might be the point at which you should learn more about optimization of sql queries on your own. There's plenty to learn, but even a little bit would help, such as a healthy use of EXPLAIN each time you make a query.
Oct 24 '06 #2

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

Similar topics

5
1626
by: Ciar?n | last post by:
I have about 7 tables I need to join, but am having a lot of difficulty with the joins, that I need some help on. I'll provide the details of four tables to illustrate the scenario. I have one table called Product, which contains a complete list of products. I have another table called Date, which contains a complete list of dates. I...
3
1452
by: Alex Glass | last post by:
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...
1
4206
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins" slows the system down considerably. I've tried creating a temp db, but I can't figure out how to execute two select commands. (It throws the exception...
7
31541
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins" slows the system down considerably. I've tried creating a temp db, but I can't figure out how to execute two select commands. (It throws the exception...
4
1636
by: trint | last post by:
Ok, This script is something I wrote for bringing up a report in reporting services and it is really slow...Is their any problems with it or is their better syntax to speed it up and still provide the same report results?: SELECT t1.MemberId, t1.PeriodID, t8.start_date, t6.amount_type_id, t6.amount_type, SUM(CASE WHEN t2.amountTypeId...
6
2404
by: Jeff Kowalczyk | last post by:
I need to adapt this an update statement to a general form that will iterate over multiple orderids for a given customerinvoiceid. My first concern is a form that will work for a given orderid, then an expanded version that will work on all orderids with a specific customerinvoiceid as a parameter. I'm sure appropriate joins will handle...
1
1560
by: database student | last post by:
Hi, I am a database Ph.D. student who is currently looking for research topics for my thesis. I am very interested in identifying and solving real problems faced by database users. Unfortunately, due to environmental constraints, I don't have much chance to interact with real world database users. Would any of you kindly let me know some...
4
1720
by: Justin Koivisto | last post by:
OK, out of my element here once again... I want a query (or something I can use as a record source for a report) to do the following: * look in 2 tables (one field each) to find all entered values (without duplicates) * count the number of records in both tables that has that value for the field.
2
3153
by: beargrease | last post by:
I'm kind of comfortable with basic joins, but a current project requires a complex query of many tables. The GROUP_CONCAT(DISTINCT ...) function has been very useful as returning my values as comma delimited values when joining multiple tables. I have one table called 'floorplans' which has two fields (floorplan_jpg & floorplan_pdf), I'd like each...
0
7612
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...
0
7924
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. ...
0
8120
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...
0
7968
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...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.