-1

I've been struggling to create the correct index to improve performance for a specific query we run on our database, I hope one of you can help point me in the right direction.

I have 2 tables as per below. I have a query to find all employees of companies that belong to a specific category with first_name like "Be%" for example. I've tried creating a multiple index on category_id, company_id but this hasn't helped. What should be the correct way to index my tables to achieve better performance for this query? Thank you in advance.

SELECT 
  e.* 
FROM employee e
INNER JOIN company c ON e.company_id = c.company_id
WHERE c.category_id = 6
AND e.first_name LIKE "Be%"
GROUP BY e.employee_id

TABLE company

| company_id | category_id |
+------------+-------------+
| ...        | ...         |
+------------+-------------+
| 47         | 6           |
+------------+-------------+
| ..         | ...         |
+------------+-------------+
| 252        | 6           |
+------------+-------------+

TABLE employee

| employee_id | company_id | first_name | ... |
+-------------+------------+------------+-----+
| 2582250     | 47         | Ben        | ... |
+-------------+------------+------------+-----+
| 3447890     | 252        | Ryan       | ... |
+-------------+------------+------------+-----+
| 7125966     | 252        | Beth       | ... |
+-------------+------------+------------+-----+
| ...         | ...        | ...        | ... |
+-------------+------------+------------+-----+

CREATE TABLES below and sqlfiddle.

CREATE TABLE company (
  `company_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `category_id` INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=InnoDB;

CREATE TABLE employee (
  `employee_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `company_id` INT UNSIGNED NOT NULL,
  `first_name` VARCHAR(255)
) ENGINE=InnoDB;
5
  • I suggest ... and ..., and ... also. Alternatively, see meta.stackoverflow.com/questions/333952/…
    – Strawberry
    Oct 12, 2017 at 20:30
  • @Strawberry thanks I've added as requested. Unfortunately this only becomes a problem with a couple millions rows which isn't feasible in sqlfiddle.
    – Martin
    Oct 12, 2017 at 20:47
  • Ok. I should have added...
    – Strawberry
    Oct 12, 2017 at 20:51
  • Problems about query performance ALWAYS require SHOW CREATE TABLE statements for all relevant tables, as well as the EXPLAIN for the given query
    – Strawberry
    Oct 12, 2017 at 20:52
  • Did you mean company_id when you wrote country_id?
    – Uueerdo
    Oct 12, 2017 at 21:55

1 Answer 1

1

You must have indexes in many:many tables. Follow the advice outlined here.

WHERE c.category_id = 6
  AND e.first_name LIKE "Be%"

needs

c: INDEX(category_id, company_id) -- which will be called for in the link above
e: INDEX(first_name, employee_id)

The Optimizer will either start with c, then reach into e. Or vice versa. I am providing you the optimal indexes for either direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.