mysql < 5.6 has terrible subquery optimization, using EXISTS / JOIN fixes this
This commit is contained in:
		
							parent
							
								
									57e5d8e538
								
							
						
					
					
						commit
						6d1c7c414b
					
				| @ -560,9 +560,22 @@ class Image { | |||||||
| 	 */ | 	 */ | ||||||
| 	public function delete_tags_from_image() { | 	public function delete_tags_from_image() { | ||||||
| 		global $database; | 		global $database; | ||||||
| 		$database->execute( | 		if($database->get_driver_name() == "mysql") { | ||||||
| 				"UPDATE tags SET count = count - 1 WHERE id IN ". | 			//mysql < 5.6 has terrible subquery optimization, using EXISTS / JOIN fixes this
 | ||||||
| 				"(SELECT tag_id FROM image_tags WHERE image_id = :id)", array("id"=>$this->id)); | 			$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)); | 		$database->execute("DELETE FROM image_tags WHERE image_id=:id", array("id"=>$this->id)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -322,8 +322,17 @@ class Index extends Extension { | |||||||
| 		// check for tags first as tag based searches are more common.
 | 		// check for tags first as tag based searches are more common.
 | ||||||
| 		if(preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { | 		if(preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { | ||||||
| 			$cmp = ltrim($matches[1], ":") ?: "="; | 			$cmp = ltrim($matches[1], ":") ?: "="; | ||||||
| 			$tags = $matches[2]; | 			$count = $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.')')); | 			$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)) { | 		else if(preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/i", $event->term, $matches)) { | ||||||
| 			$cmp = preg_replace('/^:/', '=', $matches[1]); | 			$cmp = preg_replace('/^:/', '=', $matches[1]); | ||||||
| @ -394,4 +403,3 @@ class Index extends Extension { | |||||||
| 		$this->stpen++; | 		$this->stpen++; | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 |  | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user