Heren,
Voor een webshop hebben we een query geschreven. Deze haalt uit een flinke genormeerde database gegevens op. Zo'n 20.000 producten staan erin, met bijbehorende gegevens.
Dit is de query:
Het runnen van de code:
En hier de SQL dump voor de liefhebbers:
Is dit al de maximale optimalisatie? Of hebben we een belangrijk punt over het hoofd gezien???
Voor een webshop hebben we een query geschreven. Deze haalt uit een flinke genormeerde database gegevens op. Zo'n 20.000 producten staan erin, met bijbehorende gegevens.
Dit is de query:
SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| SELECT psv.value_id, psv.type_id, filter.filter_id, filter.rename, filter.count_view, filter.count_fixed, filter.type_id, COUNT( psv.value_id ) as value_count, CONVERT(COALESCE ( psvi.value, psvf.value, psvv.value ) USING latin1) AS `value` FROM p_filter_filter as filter /* Heeft 4 records */ INNER JOIN p_specifications as ps ON filter.attribute_id = ps.attribute_id /* Heeft 21.000+ records */ INNER JOIN p_product p ON ps.product_id = p.product_id /* Heeft 5.000+ records */ INNER JOIN p_category_product as c ON ps.product_id = c.product_id /* Heeft 6.000+ records */ INNER JOIN p_staffel as s ON ps.product_id = s.product_id /* Heeft 5.000+ records */ INNER JOIN p_specifications_value as psv ON ps.spec_id = psv.spec_id /* Heeft 25.000+ records */ LEFT JOIN p_specifications_int psvi ON psvi.type_id = psv.type_id AND psvi.value_id = psv.value_id /* Heeft 6.000+ records */ LEFT JOIN p_specifications_float psvf ON psvf.type_id = psv.type_id AND psvf.value_id = psv.value_id /* Heeft 1.000+ records */ LEFT JOIN p_specifications_varchar psvv ON psvv.type_id = psv.type_id AND psvv.value_id = psv.value_id /* Heeft 18.000+ records */ GROUP BY psv.value_id, psv.type_id ORDER BY filter.index ASC , `value` ASC |
Het runnen van de code:
code:
1
| Showing rows 0 - 29 ( 125 total, Query took 0.3808 sec) |
En hier de SQL dump voor de liefhebbers:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
| -- phpMyAdmin SQL Dump -- version 3.4.9 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Nov 29, 2012 at 01:21 AM -- Server version: 5.1.66 -- PHP Version: 5.3.3 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; -- -- Database: `*` -- -- -------------------------------------------------------- -- -- Table structure for table `p_category_product` -- CREATE TABLE IF NOT EXISTS `p_category_product` ( `product_id` int(11) NOT NULL, `category_id` int(11) NOT NULL, PRIMARY KEY (`product_id`,`category_id`), KEY `category_id` (`category_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -------------------------------------------------------- -- -- Table structure for table `p_filter_filter` -- CREATE TABLE IF NOT EXISTS `p_filter_filter` ( `filter_id` int(11) NOT NULL AUTO_INCREMENT, `attribute_id` int(11) NOT NULL, `rename` varchar(50) NOT NULL, `type_id` int(11) NOT NULL, `category_id` int(11) DEFAULT NULL, `sql_id` int(11) NOT NULL, `parent_id` int(11) DEFAULT NULL, `child_visible` tinyint(1) NOT NULL, `sort_id` tinyint(1) NOT NULL, `sortable` tinyint(1) NOT NULL, `sortable_way` varchar(4) CHARACTER SET latin1 NOT NULL DEFAULT 'ASC', `count_view` tinyint(1) NOT NULL, `count_fixed` tinyint(1) NOT NULL, `hide` tinyint(1) NOT NULL, `index` int(5) NOT NULL DEFAULT '0', PRIMARY KEY (`filter_id`), KEY `attribute_id` (`attribute_id`), KEY `type_id` (`type_id`), KEY `sql_id` (`sql_id`), KEY `parent_id` (`parent_id`), KEY `sort_id` (`sort_id`), KEY `category_id` (`category_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `p_product` -- CREATE TABLE IF NOT EXISTS `p_product` ( `product_id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) DEFAULT NULL, `visibility` tinyint(1) NOT NULL DEFAULT '0', `naam` varchar(120) NOT NULL, `onderschrift` varchar(120) NOT NULL, `c_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `e_date` timestamp NULL DEFAULT NULL, PRIMARY KEY (`product_id`), KEY `user_id` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `p_specifications` -- CREATE TABLE IF NOT EXISTS `p_specifications` ( `spec_id` int(11) NOT NULL AUTO_INCREMENT, `product_id` int(11) NOT NULL, `attribute_id` int(11) NOT NULL, PRIMARY KEY (`spec_id`), KEY `product_id` (`product_id`), KEY `attribute_id` (`attribute_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `p_specifications_float` -- CREATE TABLE IF NOT EXISTS `p_specifications_float` ( `value_id` int(11) NOT NULL AUTO_INCREMENT, `type_id` tinyint(1) NOT NULL DEFAULT '3', `value` float NOT NULL, PRIMARY KEY (`value_id`), KEY `type_id` (`type_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `p_specifications_int` -- CREATE TABLE IF NOT EXISTS `p_specifications_int` ( `value_id` int(11) NOT NULL AUTO_INCREMENT, `type_id` tinyint(1) NOT NULL DEFAULT '2', `value` int(11) NOT NULL, PRIMARY KEY (`value_id`), KEY `type_id` (`type_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `p_specifications_value` -- CREATE TABLE IF NOT EXISTS `p_specifications_value` ( `id` int(11) NOT NULL AUTO_INCREMENT, `spec_id` int(11) NOT NULL, `type_id` int(11) NOT NULL, `value_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `spec_id` (`spec_id`), KEY `type_id` (`type_id`), KEY `value_id` (`value_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `p_specifications_varchar` -- CREATE TABLE IF NOT EXISTS `p_specifications_varchar` ( `value_id` int(11) NOT NULL AUTO_INCREMENT, `type_id` tinyint(1) NOT NULL DEFAULT '1', `value` varchar(255) NOT NULL, PRIMARY KEY (`value_id`), KEY `type_id` (`type_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4219 ; -- -------------------------------------------------------- -- -- Table structure for table `p_staffel` -- CREATE TABLE IF NOT EXISTS `p_staffel` ( `staffel_id` int(11) NOT NULL AUTO_INCREMENT, `from` timestamp NULL DEFAULT NULL, `too` timestamp NULL DEFAULT NULL, `product_id` int(11) NOT NULL, `count` int(10) NOT NULL, `price` double NOT NULL, PRIMARY KEY (`staffel_id`), KEY `product_id` (`product_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Constraints for dumped tables -- -- -- Constraints for table `p_category` -- ALTER TABLE `p_category` ADD CONSTRAINT `p_category_ibfk_3` FOREIGN KEY (`parent_id`) REFERENCES `p_category` (`category_id`) ON DELETE NO ACTION ON UPDATE NO ACTION; -- -- Constraints for table `p_category_product` -- ALTER TABLE `p_category_product` ADD CONSTRAINT `p_category_product_ibfk_2` FOREIGN KEY (`category_id`) REFERENCES `p_category` (`category_id`) ON DELETE CASCADE ON UPDATE NO ACTION, ADD CONSTRAINT `p_category_product_ibfk_3` FOREIGN KEY (`product_id`) REFERENCES `p_product` (`product_id`) ON DELETE NO ACTION ON UPDATE NO ACTION; -- -- Constraints for table `p_filter_filter` -- ALTER TABLE `p_filter_filter` ADD CONSTRAINT `p_filter_filter_ibfk_4` FOREIGN KEY (`parent_id`) REFERENCES `p_filter_filter` (`filter_id`) ON DELETE NO ACTION ON UPDATE NO ACTION; -- -- Constraints for table `p_product` -- ALTER TABLE `p_product` ADD CONSTRAINT `p_product_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user_login` (`user_id`) ON DELETE SET NULL ON UPDATE SET NULL; -- -- Constraints for table `p_specifications` -- ALTER TABLE `p_specifications` ADD CONSTRAINT `p_specifications_ibfk_1` FOREIGN KEY (`attribute_id`) REFERENCES `p_specifications_attributes` (`attribute_id`) ON DELETE NO ACTION ON UPDATE NO ACTION; -- -- Constraints for table `p_staffel` -- ALTER TABLE `p_staffel` ADD CONSTRAINT `p_staffel_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `p_product` (`product_id`) ON DELETE CASCADE ON UPDATE NO ACTION; |
Is dit al de maximale optimalisatie? Of hebben we een belangrijk punt over het hoofd gezien???
[ Voor 5% gewijzigd door RobIII op 29-11-2012 10:09 . Reden: Query leesbaar gemaakt ]
Professioneel Heftruck Syndroom