I'm using mysql version 5.6.47. I have the following table for student marks:
CREATE TABLE `studentmarks` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`StudentID` int(11) NOT NULL,
`subjectName` varchar(255) DEFAULT NULL,
`MARKS` int(11) NOT NULL,
PRIMARY KEY (`ID`),
KEY `idx_studentmarks_StudentID` (`StudentID`)
);
and created a View on the table:
CREATE OR REPLACE VIEW `vw_student_marks` AS
SELECT
`s1`.`StudentID` AS `StudentID`,
`s1`.`subjectName` AS `subjectName`,
`s1`.`MARKS` AS `marks`,
(SELECT
SUM(`s2`.`MARKS`)
FROM
`studentmarks` `s2`
WHERE
(`s2`.`StudentID` = `s1`.`StudentID`)) AS `totalMarks`
FROM
`studentmarks` `s1`;
When testing with around 20K rows, there is a noticeable difference in performance in running SELECT query
vs SELECT * FROM VIEW
. The select query shows an optimized execution plan with just 1 full table scan, while for view there are 2 full table scans.
Query Stats (Measured by MySQL Workbench):
SELECT QUERY
Timing: 0:00:0.07677120 (as measured by the server)
Rows Examined: 108285
SELECT FROM VIEW QUERY :
Timing: 0:00:1.6082441 (as measured by the server)
Rows Examined: 2985730
What is the reason behind this difference in performance?
Query Execution Plans: https://i.stack.imgur.com/noOxI.jpg
UPDATE: I tested with MySQL version 8.0.19, the same issue occurs
MySQL must be using TEMPTABLE algorithm for the view in this case (aggregate functions). This might be the reason for the difference.
You can refer https://dev.mysql.com/doc/refman/5.6/en/view-algorithms.html for more details.