php - 在 php 中查询非常慢(>30 秒),但在 phpmyadmin 中运行查询时速度很快

标签 php mysql database phpmyadmin

我认为自己是 mysql/php 的初学者,当你看到我的代码时不要感到震惊:D。我用 mysql 查询创建了一个 php 函数,当我调用该函数时,它需要很长时间(大多数时间)。他们很少会被快速处决。在 PHPmyadmin 中,该 php 函数中的单个查询运行速度快且没有错误。

慢速函数

       public function getAllMusicDataNew($playlist_name, $mysqli) {

// einzeln alle titel / alle künstler / alle alben auflisten als array 

    $sql1 = "Select distinct title, artist, album from songs where id not in(Select distinct id from songs where id 
    IN(Select id from songs where title IN(Select title from songs where id 
    IN(Select song_id from playlist where playlist_id IN (Select playlist_id from playlists where name = '$playlist_name')))))";
    $getTitles = $mysqli->query($sql1);

    $sql2 = "Select distinct artist from songs where id not in(Select distinct id from songs where id IN
    (Select id from songs where artist IN(Select artist from songs where id IN(Select song_id from playlist
    where playlist_id IN (Select playlist_id from playlists where name = '$playlist_name')))))";
    $getArtists = $mysqli->query($sql2);

    $sql3 = "Select distinct album, artist from songs where id not in( Select distinct id from songs where id IN(Select id from songs where album IN( Select album from songs where id IN( Select song_id from playlist
    where playlist_id IN ( Select playlist_id from playlists where name = '$playlist_name'))and album <> '')))";
    $getAlbums = $mysqli->query($sql3);

    $sql4 = "Select distinct title, artist, album from songs where id in(Select distinct id from songs where id 
    IN(Select id from songs where title IN(Select title from songs where id 
    IN(Select song_id from playlist where playlist_id IN (Select playlist_id from playlists where name = '$playlist_name')))))";
    $getTitles_added = $mysqli->query($sql4);

    $sql5 = "Select distinct artist from songs where id  in(Select distinct id from songs where id IN
    (Select id from songs where artist IN(Select artist from songs where id IN(Select song_id from playlist
    where playlist_id IN (Select playlist_id from playlists where name = '$playlist_name')))))";
    $getArtists_added = $mysqli->query($sql5);

    $sql6 = "Select distinct album, artist from songs where id in( Select distinct id from songs where id IN(Select id from songs where album IN( Select album from songs where id IN( Select song_id from playlist
    where playlist_id IN ( Select playlist_id from playlists where name = '$playlist_name'))and album <> '')))";
    $getAlbums_added = $mysqli->query($sql6);


while ($row = $getTitles->fetch_assoc()) {

      $results_array1[] = $row;


    }

        while ($row = $getArtists->fetch_assoc()) {

      $results_array2[] = $row;


    }

        while ($row = $getAlbums->fetch_assoc()) {

      $results_array3[] = $row;


    }


    if(mysqli_num_rows($getTitles_added)!=0) {


        while ($row = $getTitles_added->fetch_assoc()) {

      $results_array4[] = $row;


    }


        while ($row = $getArtists_added->fetch_assoc()) {

      $results_array5[] = $row;


    }
    while ($row = $getAlbums_added->fetch_assoc()) {

      $results_array6[] = $row;


    }

    } else {


      $results_array4[] = "empty";

      $results_array5[] = "empty";

      $results_array6[] = "empty";


     }
//  print_r($titles);
        $titles["titles"] = $results_array1;
    $artists["artists"] = $results_array2;
    $albums["albums"] = $results_array3;
    $titles_added["titles_added"] = $results_array4;
    $artists_added["artists_added"] = $results_array5;
    $albums_added["albums_added"] = $results_array6;


        //  printf($array_merge($results_array1, $results_array2,      $results_array3));
            return array_merge($titles, $artists, $albums, $titles_added, $artists_added, $albums_added);


}

数据库

-- phpMyAdmin SQL Dump
-- version 4.2.12deb2+deb8u1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Erstellungszeit: 10. Aug 2016 um 17:04
-- Server Version: 5.5.44-0+deb8u1
-- PHP-Version: 5.6.23-0+deb8u1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Datenbank: `musicbox2`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `playlist`
--

CREATE TABLE IF NOT EXISTS `playlist` (
  `playlist_id` int(11) NOT NULL,
`track_id` int(11) NOT NULL,
  `song_id` int(11) NOT NULL,
  `votes` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1189 DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `playlists`
--

CREATE TABLE IF NOT EXISTS `playlists` (
`playlist_id` int(11) NOT NULL,
  `name` varchar(60) NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `songs`
--

CREATE TABLE IF NOT EXISTS `songs` (
`id` int(11) NOT NULL,
  `path` varchar(100) NOT NULL,
  `artist` varchar(60) NOT NULL,
  `title` varchar(60) NOT NULL,
  `album` varchar(50) NOT NULL,
  `added_at` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4803 DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `users`
--

CREATE TABLE IF NOT EXISTS `users` (
`userID` int(11) NOT NULL,
  `voices` int(11) NOT NULL,
  `pass` varchar(18) NOT NULL,
  `created_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `playlist`
--
ALTER TABLE `playlist`
 ADD PRIMARY KEY (`track_id`), ADD KEY `song_id` (`song_id`), ADD KEY `playlist_id` (`playlist_id`);

--
-- Indizes für die Tabelle `playlists`
--
ALTER TABLE `playlists`
 ADD PRIMARY KEY (`playlist_id`);

--
-- Indizes für die Tabelle `songs`
--
ALTER TABLE `songs`
 ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `title` (`title`,`artist`,`album`);

--
-- Indizes für die Tabelle `users`
--
ALTER TABLE `users`
 ADD PRIMARY KEY (`userID`);

--
-- AUTO_INCREMENT für exportierte Tabellen
--

--
-- AUTO_INCREMENT für Tabelle `playlist`
--
ALTER TABLE `playlist`
MODIFY `track_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1189;
--
-- AUTO_INCREMENT für Tabelle `playlists`
--
ALTER TABLE `playlists`
MODIFY `playlist_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=13;
--
-- AUTO_INCREMENT für Tabelle `songs`
--
ALTER TABLE `songs`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4803;
--
-- AUTO_INCREMENT für Tabelle `users`
--
ALTER TABLE `users`
MODIFY `userID` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints der exportierten Tabellen
--

--
-- Constraints der Tabelle `playlist`
--
ALTER TABLE `playlist`
ADD CONSTRAINT `playlist_ibfk_1` FOREIGN KEY (`song_id`) REFERENCES `songs` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `playlist_ibfk_2` FOREIGN KEY (`playlist_id`) REFERENCES `playlists` (`playlist_id`) ON DELETE CASCADE;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Sql Process list

最佳答案

使用这样的连接查询,我得到了相同的结果及其工作:)仍然不知道为什么嵌套查询需要长达 40-50 秒的时间。 感谢用户 Dibakar Paul 帮助了我!

$sql1 = "Select distinct title, artist, album 
from songs where id not in(Select distinct song.id
from songs song inner join playlist playlist 
on playlist.song_id=song.id
inner join playlists playlists 
on playlists.playlist_id=playlist.playlist_id
and playlists.name = '$playlist_name')";

$sql2 = "Select distinct song.title, song.artist, song.album
from songs song inner join playlist playlist 
on playlist.song_id=song.id
inner join playlists playlists 
on playlists.playlist_id=playlist.playlist_id
and playlists.name = '$playlist_name'";

关于php - 在 php 中查询非常慢(>30 秒),但在 phpmyadmin 中运行查询时速度很快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877348/

相关文章:

php - 使用单选按钮将行插入数据库

mysql - 使用 SQL REPLACE 修复字符编码错误是否可以?

python - 值错误 : "..." needs to have a value for field "..." before this many-to-many relationship can be used

mysql - 在mysql数据库中获取多行

MySQL 与 PostgreSQL?我应该为我的 Django 项目选择哪个?

php - 如何使用 PLupload 发送附加数据?

php - Yii2:从小部件内渲染 View

mysql - 错误 : #1071 - Specified key was too long; max key length is 1000 bytes - mysql 5. 0.91

mysql - 计算同一成员表中 2 个日期之间的推荐新成员

PHP 日期 - 从格式 W-m-Y 获取给定周的开始和结束日期