pro*_*lin 6 php mysql sql database
<?php
$db = new mysqli(//editted out db credentials);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT m.ID, m.Title, GROUP_CONCAT(a.Title) AS Artist
FROM mp3s m
LEFT JOIN artist_relations ar ON ar.mp3ID = m.ID
LEFT JOIN artists a ON a.ID = ar.artistID
GROUP BY m.ID
ORDER BY ID
LIMIT 0,30;
";
if($result = $db->query($sql)){
echo "<table>";
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$row['Title']."</td>";
echo "<td>".$row['Artist']."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Run Code Online (Sandbox Code Playgroud)
该查询正常工作,但是速度非常慢。
该数据库具有3个字段:
`artists` : ID , Title
`mp3s` : ID , Title
`artist_relations` : mp3ID , artistID
Run Code Online (Sandbox Code Playgroud)
我需要这个:
row1: titlemusic1 - artist1 , artist4 , artist5
row2: titlemusic2 - artist1
row3: titlemusic1 - artist3 , artist8
row4: titlemusic1 - artist9 , artist10
...
Run Code Online (Sandbox Code Playgroud)
mp3,artist和artist_relations每个都有超过20000个记录
EXPLAIN SELECT m.ID, m.Title, .... :
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE m ALL NULL NULL NULL NULL 23718 Using temporary; Using filesort
1 SIMPLE ar ALL NULL NULL NULL NULL 24337
1 SIMPLE a eq_ref PRIMARY PRIMARY 4 ganools_rj.ar.artistID 1
Run Code Online (Sandbox Code Playgroud)
-
artist_relations如果不存在则创建表artist_relations(
artistIDint(11)NOT NULL,
mp3IDint(11)NOT NULL)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-
artists如果不存在则创建表artists(
IDint(11)NOT NULL AUTO_INCREMENT,
Titlevarchar(155)NOT NULL PRIMARY KEY(ID))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 9005;
-
mp3s如果不存在则创建表mp3s(
IDint(11)NOT NULL AUTO_INCREMENT,
Titlevarchar(155)NOT NULL,
imageURLvarchar(155)NOT NULL,
mp3URLvarchar(155)NOT NULL,
Descriptiontext,
Lyrictext,
albumvarchar(155)DEFAULT NULL,
playsint(11) DEFAULT NULL,
pubDate日期NOT NULL,PRIMARY KEY(ID))ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 22936;