Inspired by this article I tried optimizing an export function of a memberlist from our database.
The original queries look like this:
-- First query
SELECT
*
FROM
members_customer_id_0000000169
WHERE
deleted = '0000-00-00 00:00:00'
-- Second query run for every result from first query
SELECT
stores.external_store_id AS local_store_id
FROM
regions,
stores,
stores_reference_members_customer_id_0000000169
WHERE
regions.customer_id = 169
AND
stores.region_id = regions.id
AND
stores_reference_members_customer_id_0000000169.member_id =' . $result['id'] . '
AND
stores_reference_members_customer_id_0000000169.store_id = stores.id
The first query returns 180k rows and its performance is not efficient the second query fetch all 180k times.
I tried doing this instead:
SELECT
members_customer_id_0000000169.*,
stores.external_store_id AS local_store_id
FROM
members_customer_id_0000000169
LEFT JOIN
stores_reference_members_customer_id_0000000169
ON
stores_reference_members_customer_id_0000000169.member_id = members_customer_id_0000000169.id
JOIN
regions
JOIN
stores
WHERE
members_customer_id_0000000169.deleted = '0000-00-00 00:00:00'
AND
regions.customer_id = 169
AND
stores.region_id = regions.id
AND
stores_reference_members_customer_id_0000000169.store_id = stores.id
The result is only 140k rows. The reason is probably that I don’t get members without a local_store_id as I intended.
And I’m unsure what the problem is.
I’m confident that I’m doing something wrong with the joins because of the many tables and WHERE clauses, but I only have experience with doing LEFT/RIGHT joins between two tables.
Can anyone identify the problem?
Each JOIN operation should have a corresponding ON statement to tell SQL how to merge the records from the new table. It seems like you are doing some of that logic in the WHERE statement, which could be causing the issue.
Also, as far as getting members without a local_store_id, it could be the fact that you are doing an inner join on stores, so your result set only has members that have a corresponding row in stores based on your join logic. A left outer join may give you what you’re looking for.