반응형
mysql 쿼리에 이상한 성능 문제가 있습니다.
SELECT
`pricemaster_products`.*,
`products`.*
FROM `pricemaster_products`
LEFT JOIN `products`
ON `pricemaster_products`.`ean` = `products`.`products_ean`
왼쪽 조인을 명시 적으로 사용하고 싶습니다. 그러나 쿼리는 필요한 것보다 훨씬 더 많은 시간이 걸립니다.
조인을 INNER JOIN으로 변경하려고했습니다. 이제 쿼리는 정말 빠르지 만 결과는 내가 필요한 것이 아닙니다.
나는 Explain을 사용하여 다음과 같은 결론에 도달했습니다.
"LEFT JOIN"을 사용하면 쿼리의 EXPLAIN 결과가 ...
type: "ALL"
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 90.000 / 50.000 (the full number of the corresponding table)
... 두 테이블 모두.
"INNER JOIN"을 사용하면 EXPLAIN은 다음을 제공합니다.
테이블 "제품":
Same result as above.
"pricemaster_products"테이블의 경우 :
type: "ref"
possible_keys: "ean"
key: ean
key_len: 767
ref: func
rows: 1
extra: using where
두 테이블 모두 관련 열에 설정된 인덱스가 있습니다. LEFT JOIN이 너무 느리다고 생각할 수있는 유일한 이유는 인덱스를 전혀 사용하지 않기 때문입니다. 하지만 왜 그렇지 않을까요?
테이블 구조는 다음과 같습니다.
CREATE TABLE IF NOT EXISTS `pricemaster_products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`provider` varchar(255) CHARACTER SET utf8 NOT NULL,
`ean` varchar(255) CHARACTER SET utf8 NOT NULL,
`title` varchar(255) CHARACTER SET utf8 NOT NULL,
`gnp` double DEFAULT NULL,
`vat` int(11) DEFAULT NULL,
`cheapest_price_with_shipping` double DEFAULT NULL,
`last_cheapest_price_update` int(11) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ean` (`ean`),
KEY `title` (`title`),
KEY `gnp` (`gnp`),
KEY `vat` (`vat`),
KEY `provider` (`provider`),
KEY `cheapest_price_with_shipping` (`cheapest_price_with_shipping`),
KEY `last_cheapest_price_update` (`last_cheapest_price_update`),
KEY `active` (`active`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=58436 ;
CREATE TABLE IF NOT EXISTS `products` (
`products_id` int(11) NOT NULL AUTO_INCREMENT,
`products_ean` varchar(128) DEFAULT NULL,
`products_status` tinyint(1) NOT NULL DEFAULT '1',
[a lot more of fields with no connection to the query in question]
PRIMARY KEY (`products_id`),
KEY `products_status` (`products_status`),
KEY `products_ean` (`products_ean`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=105518 ;
해결 방법
조인에 대한 두 개의 관련 필드가 정확히 동일한 유형 (CHARACTER SET utf8의 varchar (255) 및 latin1의 varchar (128))을 갖지 않았습니다. 동일한 길이와 문자 집합을 모두 설정했으며 이제 LEFT JOIN을 사용하는 쿼리가 예상대로 작동합니다.
참조 페이지 https://stackoverflow.com/questions/18660252
반응형
'MySql' 카테고리의 다른 글
MySQL Symfony2 Doctrine을 사용하여 기존 데이터베이스에서 엔티티 가져 오기 (0) | 2020.12.23 |
---|---|
MySQL Doctrine2-한 번에 여러 삽입 (0) | 2020.12.23 |
MySQL What is the best datatype for storing URLs in a MySQL database? (0) | 2020.12.23 |
MySQL MYSQL datetime 열에 삽입 할 datetime c #의 형식 문자열 (0) | 2020.12.23 |
MySQL how to export a mysql data table search query into csv using php? (0) | 2020.12.22 |
댓글