473,385 Members | 1,908 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,385 software developers and data experts.

Help with Indexes, Unions and Joins greatly appreciated!

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 1322
miller
1,089 Expert 1GB
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
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...
3
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....
1
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"...
7
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"...
4
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...
6
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,...
1
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....
4
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...
2
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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,...

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.