mysql < 5.6 has terrible subquery optimization, using EXISTS / JOIN fixes this

This commit is contained in:
Daku 2015-12-04 11:38:44 +00:00
parent 57e5d8e538
commit 6d1c7c414b
2 changed files with 30 additions and 9 deletions

View File

@ -560,9 +560,22 @@ class Image {
*/
public function delete_tags_from_image() {
global $database;
$database->execute(
"UPDATE tags SET count = count - 1 WHERE id IN ".
"(SELECT tag_id FROM image_tags WHERE image_id = :id)", array("id"=>$this->id));
if($database->get_driver_name() == "mysql") {
//mysql < 5.6 has terrible subquery optimization, using EXISTS / JOIN fixes this
$database->execute("
UPDATE tags t
INNER JOIN image_tags it ON t.id = it.tag_id
SET count = count - 1
WHERE it.image_id = :id",
array("id"=>$this->id)
);
} else {
$database->execute("
UPDATE tags
SET count = count - 1
WHERE id IN (SELECT tag_id FROM image_tags WHERE image_id = :id)", array("id"=>$this->id)
);
}
$database->execute("DELETE FROM image_tags WHERE image_id=:id", array("id"=>$this->id));
}

View File

@ -322,8 +322,17 @@ class Index extends Extension {
// check for tags first as tag based searches are more common.
if(preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$tags = $matches[2];
$event->add_querylet(new Querylet('images.id IN (SELECT DISTINCT image_id FROM image_tags GROUP BY image_id HAVING count(image_id) '.$cmp.' '.$tags.')'));
$count = $matches[2];
$event->add_querylet(
new Querylet("EXISTS (
SELECT 1
FROM image_tags it
LEFT JOIN tags t ON it.tag_id = t.id
WHERE images.id = it.image_id
GROUP BY image_id
HAVING COUNT(*) $cmp $count
)")
);
}
else if(preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/i", $event->term, $matches)) {
$cmp = preg_replace('/^:/', '=', $matches[1]);
@ -394,4 +403,3 @@ class Index extends Extension {
$this->stpen++;
}
}