There are 3 tables and I linked three tables with one query. But the problem is that the result returns in 7-8 seconds. One of the columns I join is JSON type and the data in it is json. For this reason, I did the join process with JSON. How can I make the following query faster? Or can I achieve the same result using another method?
Here is a table example for sales with json column:
And the query:
SELECT
`s`.`id` AS `id`,
`s`.`status` AS `status`,
group_concat(`c`.`display_name` separator ', ') AS `buyer`,
`u`.`display_name` AS `partner`,
`s`.`sales_price` AS `sales_price`,
FROM `sales` AS `s`
LEFT JOIN `users` AS `u` ON (`u`.`id` = `s`.`user_id`)
JOIN `contacts` AS `c` ON (json_contains(`s`.`buyers`, concat('"', `c`.`id`, '"')))
GROUP BY
`s`.`id`
My tables:
SALES
column_name | column_type |
---|---|
id | integer |
buyers | varchar(255) |
user_id | integer |
status | integer |
sales_price | double(13,2) |
CONTACTS
column_name | column_type |
---|---|
id | integer |
display_name | varchar(255) |
USERS
column_name | column_type |
---|---|
id | integer |
display_name | varchar(255) |
SALES.user_id => USERS.id SALES.buyers => CONTACTS.id
Sales.buyers can be multiple or single like this:
If the sale has one contact: ["774"] or If the sale has two contacts: ["774", "854"]
I want to see this output:
S.id | S.status | C.display_name AS BUYER | U.display_name AS PARTNER | S.sales_price |
---|---|---|---|---|
1 | 1 | Michael Owen | David Beckham | 199999 |
2 | 1 | Ariel Ortega, Mauro Icardi | Leo Messi | 219000 |
3 | 0 | Nicholas Anelka, Didier Drogba | Thierry Henry | 710000 |
EXPLAIN result is here:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | PRIMARY | ALL | 12455638 | ||||||
2 | DERIVED | s | ALL | 857 | Using temporary; Using filesort | ||||
2 | DERIVED | u | eq_ref | PRIMARY | PRIMARY | 4 | s.user_id | 1 | |
2 | DERIVED | c | ALL | 14533 | Using where; Using join buffer (flat, BNL join) | ||||
3 | UNION | s2 | ALL | buyers_index | 857 | Using where | |||
3 | UNION | u2 | eq_ref | PRIMARY | PRIMARY | 4 | s2.user_id | 1 | |
UNION RESULT | <union2,3> | ALL |