我的查询中有多个连接集。我有多个连接,因为表的结构可以得到我想要的。我的主要加入,我分为三组,如下所述。如果我只有一套,查询时间是相当快的。当我有两套活动时,查询时间约为 2 分钟。如果如下图所有三组都处于活动状态,则需要的时间太长。
任何有关优化此查询的帮助将不胜感激。
$query = "SELECT `Databases`.*,
`DatabaseDescriptors`.*,
`DatabaseContents`.*,
`DatabaseAccessLevels`.*,
`Providers`.*,
GROUP_CONCAT(`Descriptors`.DescriptorName SEPARATOR ', ') as DescriptorNames,
GROUP_CONCAT(`Contents`.ContentName SEPARATOR ', ') as ContentNames,
GROUP_CONCAT(`AccessLevels`.AccessLevelName SEPARATOR ', ') as AccessLevelNames ";
$query .= "FROM `Databases` ";
// SET 1
$query .= "JOIN `DatabaseDescriptors`
ON `DatabaseDescriptors`.DatabaseID = `Databases`.DatabaseID ";
$query .= "JOIN `Descriptors`
ON `Descriptors`.DescriptorID = `DatabaseDescriptors`.DescriptorID ";
//SET 2
$query .= "JOIN `DatabaseContents`
ON `DatabaseContents`.DatabaseID = `Databases`.DatabaseID ";
$query .= "JOIN `Contents`
ON `Contents`.ContentID = `DatabaseContents`.ContentID ";
//SET 3
$query .= "JOIN `DatabaseAccessLevels`
ON `DatabaseAccessLevels`.DatabaseID = `Databases`.DatabaseID ";
$query .= "JOIN `AccessLevels`
ON `AccessLevels`.AccessLevelID = `DatabaseAccessLevels`.AccessLevelID ";
$query .= "JOIN `Providers`
ON `Providers`.ProviderID = `Databases`.ProviderID ";
$query .= "AND `Databases`.DatabaseID = 47";