I am trying to optimize the query
SELECT
CONCAT(lastname, ', ', title, ' ', firstname) AS fullName,
e.lastname,
e.firstname,
e.email,
e.userType,
e.userID
FROM
employee e,
employee_categories ec
WHERE
1 = 1
which is taking 20 seconds to run. The employee table has 6000 records(number of employees) and employee_categories table has 1000 records. Both tables are indexed and index is the primary key in both tables. The index type is unique and index method is BTREE.
I tried
SELECT
SQL CACHE CONCAT(lastname, ', ', title, ' ', firstname) AS fullName,
e.lastname,
e.firstname,
e.email,
e.userType,
e.userID
FROM
employee e,
employee_categories ec
WHERE
1 = 1
I set query_cache_size to 32MB, ensured have_query_cache is set to Yes, query_cache_type is set to 1. I am using MySql 5.5.24 with InnoDB as storage engine
What else can I try?
Any suggestions would be appreciated.
-
Do you really mean to do a Cartesian product of the two tables? Your result will have 6 million rows, so 20 seconds seems very reasonable!Colin 't Hart– Colin 't Hart2015年04月10日 08:08:40 +00:00Commented Apr 10, 2015 at 8:08
1 Answer 1
Without some way of tying the two tables together, you are asking for 6 million rows (6000*1000).
Instead of FROM employee e, employee_categories ec
, you need something like
FROM employee e
JOIN employee_categories ec ON e.category = ec.category
AND... be sure category
(or whatever it is called) is indexed (perhaps PRIMARY KEY
) in one of the tables.
Those changes should speed it up by a factor of 1000 or so.
The Query cache is irrelevant (and generally should be turned off).
-
Thanks a lot Rick. I had to do a left join, but now the query runs in 4 seconds. Thanks again.Chris H– Chris H2015年04月13日 03:06:26 +00:00Commented Apr 13, 2015 at 3:06
-
Glad you are happy. I was expecting much more than 5x improvement. (Although, I did not really expect 1000x.)Rick James– Rick James2015年04月14日 05:41:48 +00:00Commented Apr 14, 2015 at 5:41
-
Thanks Rick, I used Query cache thinking it would speed up things and found that when searching for optimizing a query on MySQL database. I will need to search and read some blogs/books on tuning databases. Would you recommend any? Thanks for helping the community.Chris H– Chris H2015年04月20日 01:38:10 +00:00Commented Apr 20, 2015 at 1:38
-
There are a number of good books out there, but I am biased toward my blogs which cover a number of common issues. The blogs on
Memory allocation
(which includes Query cache) andCookbook for Creating Indexes
are perhaps most relevant to this thread.Rick James– Rick James2015年04月20日 03:14:50 +00:00Commented Apr 20, 2015 at 3:14
Explore related questions
See similar questions with these tags.