powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / MySQL [игнор отключен] [закрыт для гостей] / Помогите оптимизировать запрос
2 сообщений из 2, страница 1 из 1
Помогите оптимизировать запрос
    #38332808
CLilian
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Привет всем,
надо оптимизировать запрос, сильно время растет если я ставлю ORDER BY в конце запроса от 31ms до 15sec, и это учитывая что я показываю всего 50 записей из базы.
Помогите пожайлуста решить данную задачу, я почти уверен что чтото я неправильно делаю, опыта в запросах у меня небольшой.
Вот сам запрос:
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
SELECT 
  descriptions.identify,
  CONCAT('[', categories.code, '] ', categories.name) AS category,
  CONCAT(categories.code, ' ', identifications.number) AS number,
  (SELECT users.caption FROM users WHERE users.identify = descriptions.owner LIMIT 1) AS owner,
  identifications.folder,
  CONCAT('(', countries.name, ') ', locations.name) AS location,
  IF(descriptions.owner = ".$_SESSION["identify"].", 'N', 'Y') AS blocked,
  IFNULL((SELECT COUNT(files.identify) FROM files WHERE files.identification = descriptions.identification GROUP BY files.identification LIMIT 1), 0) AS attached,
  countries.name,
  descriptions.title,
  descriptions.recorded,
  descriptions.encoded,
  descriptions.timekeeping,
  descriptions.notes,
  descriptions.published,
  IFNULL((SELECT GROUP_CONCAT(CONCAT('<b>', characters.name, ':</b> ', styles.name) ORDER BY characters.name SEPARATOR '<br>')
  FROM styles INNER JOIN creations ON (styles.identify = creations.style) INNER JOIN characters ON (styles.character = characters.identify) 
  WHERE creations.description = descriptions.identify GROUP BY creations.description), '') AS d_creations,
  IFNULL((SELECT GROUP_CONCAT(CONCAT('<b>', languages.name, ':</b> ', indications.notes) ORDER BY languages.code SEPARATOR '<br>')
  FROM indications INNER JOIN languages ON (indications.language = languages.identify)
  WHERE indications.description = descriptions.identify GROUP BY indications.description), '') AS d_indications,
  IFNULL((SELECT GROUP_CONCAT(CONCAT('<b>', contributions.name, ':</b> ', CONCAT(people.first_name, ' ', people.last_name, ', <span style=\'color:#747474\'>[n&#259;scut(&#259;)', IF(people.birthyear <> '', CONCAT(' la ', people.birthyear), ''), ' &#238;n ', countries.name, ']</span>')) ORDER BY countries.code SEPARATOR '<br>')
  FROM people INNER JOIN collaborations ON (people.identify = collaborations.person) INNER JOIN countries ON (people.country = countries.identify) INNER JOIN contributions ON (collaborations.contribution = contributions.identify)
  WHERE collaborations.description = descriptions.identify GROUP BY collaborations.description), '') AS d_collaborations,
  IFNULL((SELECT GROUP_CONCAT(CONCAT('<b>', contributions.name, ':</b> ', CONCAT(teams.name, ', <span style=\'color:#747474\'>[din ', countries.name, IF(IFNULL(tools.name, '') <> '', CONCAT(', c&#226;nt&#259; la ', tools.name), ''), ']</span>')) ORDER BY countries.code SEPARATOR '<br>')
  FROM teams INNER JOIN countries ON (countries.identify = teams.country) LEFT JOIN tools ON (teams.tool = tools.identify) INNER JOIN interpretations ON (teams.identify = interpretations.team) INNER JOIN contributions ON (interpretations.contribution = contributions.identify)
  WHERE interpretations.description = descriptions.identify GROUP BY interpretations.description), '') AS d_interpretations
FROM 
  identifications
  LEFT JOIN descriptions ON (identifications.identify = descriptions.identification)
  LEFT JOIN categories ON (identifications.category = categories.identify)
  LEFT JOIN locations ON (descriptions.location = locations.identify)
  LEFT JOIN countries ON (locations.country = countries.identify) 
ORDER BY
  categories.code,
  identifications.number
LIMIT 
  0, 50;


Индексы у меня стоят в таблицах:
categories: code, name
identifications: category, owner, number
descriptions: identification, owner, location
locations: country, name
...
Рейтинг: 0 / 0
Помогите оптимизировать запрос
    #38333004
Фотография javajdbc
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
CLilian,

вам надо первым сделать такой СКЛ.

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
select * 
FROM 
  identifications
  LEFT JOIN categories ON (identifications.category = categories.identify)
ORDER BY
  categories.code,
  identifications.number
LIMIT 
  0, 50;



для него нужен индекс на categories.identify.


Потом сделаете это основным селектом и подцепите все остальное.
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / MySQL [игнор отключен] [закрыт для гостей] / Помогите оптимизировать запрос
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]