1 | <?php |
---|
2 | |
---|
3 | $link = mysql_connect( 'localhost', 'root', 'root' ); |
---|
4 | mysql_select_db( 'test', $link ); |
---|
5 | |
---|
6 | $tables = array( |
---|
7 | 'test100' => 100, |
---|
8 | 'test1k' => 1000, |
---|
9 | 'test10k' => 10000, |
---|
10 | 'test100k' => 100000, |
---|
11 | 'test1m' => 1000000 |
---|
12 | ); |
---|
13 | |
---|
14 | $engines = array( 'innodb', 'myisam' ); |
---|
15 | $percentages = array( 50, 20, 5 ); |
---|
16 | |
---|
17 | // a is like option_name, b is like option_value, c is like autoload |
---|
18 | $cols = "( a VARCHAR(255), b LONGTEXT, c VARCHAR(20) )"; |
---|
19 | |
---|
20 | $a = str_repeat( 'x', 100 ); |
---|
21 | $b = str_repeat( 'x', 1000 ); |
---|
22 | |
---|
23 | $previous_table = ''; |
---|
24 | |
---|
25 | foreach ( $tables as $table => $rows ) { |
---|
26 | echo "Creating $table...\n"; |
---|
27 | |
---|
28 | mysql_query( "DROP TABLE IF EXISTS {$table}_innodb_50" ); |
---|
29 | mysql_query( "DROP TABLE IF EXISTS {$table}_innodb_20" ); |
---|
30 | mysql_query( "DROP TABLE IF EXISTS {$table}_innodb_5" ); |
---|
31 | |
---|
32 | mysql_query( "DROP TABLE IF EXISTS {$table}_myisam_50" ); |
---|
33 | mysql_query( "DROP TABLE IF EXISTS {$table}_myisam_20" ); |
---|
34 | mysql_query( "DROP TABLE IF EXISTS {$table}_myisam_5" ); |
---|
35 | |
---|
36 | mysql_query( "CREATE TABLE {$table}_innodb_50 $cols ENGINE=InnoDB;" ); |
---|
37 | mysql_query( "CREATE TABLE {$table}_innodb_20 $cols ENGINE=InnoDB;" ); |
---|
38 | mysql_query( "CREATE TABLE {$table}_innodb_5 $cols ENGINE=InnoDB;" ); |
---|
39 | |
---|
40 | mysql_query( "CREATE TABLE {$table}_myisam_50 $cols ENGINE=MyISAM;" ); |
---|
41 | mysql_query( "CREATE TABLE {$table}_myisam_20 $cols ENGINE=MyISAM;" ); |
---|
42 | mysql_query( "CREATE TABLE {$table}_myisam_5 $cols ENGINE=MyISAM;" ); |
---|
43 | |
---|
44 | |
---|
45 | echo "Filling tables"; |
---|
46 | |
---|
47 | if ( $rows < 100000 ) { |
---|
48 | for ( $ii = 0; $ii < $rows; $ii++ ) { |
---|
49 | $val = "no"; |
---|
50 | if ( rand( 0, 99 ) < 50 ) { |
---|
51 | $val = "yes"; |
---|
52 | } |
---|
53 | mysql_query( "INSERT INTO {$table}_innodb_50 VALUES('$a', '$b', '$val');" ); |
---|
54 | |
---|
55 | $val = "no"; |
---|
56 | if ( rand( 0, 99 ) < 20 ) { |
---|
57 | $val = "yes"; |
---|
58 | } |
---|
59 | mysql_query( "INSERT INTO {$table}_innodb_20 VALUES('$a', '$b', '$val');" ); |
---|
60 | |
---|
61 | $val = "no"; |
---|
62 | if ( rand( 0, 99 ) < 5 ) { |
---|
63 | $val = "yes"; |
---|
64 | } |
---|
65 | mysql_query( "INSERT INTO {$table}_innodb_5 VALUES('$a', '$b', '$val');" ); |
---|
66 | |
---|
67 | if ( $ii && ! ( $ii % ( $rows / 10 ) ) ) { |
---|
68 | echo '.'; |
---|
69 | } |
---|
70 | } |
---|
71 | } else { |
---|
72 | // For the huge tables, it takes way too long to generate random rows, |
---|
73 | // so we approximate it by taking multiple copies of previously |
---|
74 | // generated tables, instead. |
---|
75 | for ( $ii = 0; $ii < $rows; $ii += $rows/10 ) { |
---|
76 | mysql_query( "INSERT INTO {$table}_innodb_50 SELECT * FROM {$previous_table}_innodb_50;" ); |
---|
77 | mysql_query( "INSERT INTO {$table}_innodb_20 SELECT * FROM {$previous_table}_innodb_20;" ); |
---|
78 | mysql_query( "INSERT INTO {$table}_innodb_5 SELECT * FROM {$previous_table}_innodb_5;" ); |
---|
79 | echo '.'; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | echo "\nCopying tables...\n"; |
---|
84 | mysql_query( "INSERT INTO {$table}_myisam_50 SELECT * FROM {$table}_innodb_50;" ); |
---|
85 | mysql_query( "INSERT INTO {$table}_myisam_20 SELECT * FROM {$table}_innodb_20;" ); |
---|
86 | mysql_query( "INSERT INTO {$table}_myisam_5 SELECT * FROM {$table}_innodb_5;" ); |
---|
87 | |
---|
88 | $previous_table = $table; |
---|
89 | } |
---|
90 | |
---|
91 | $times = array(); |
---|
92 | foreach ( $tables as $table => $row ) { |
---|
93 | $times[ $table ] = array(); |
---|
94 | foreach ( $engines as $engine ) { |
---|
95 | $times[ $table ][ $engine ] = array(); |
---|
96 | foreach( $percentages as $percentage ) { |
---|
97 | $times[ $table ][ $engine ][ $percentage ] = array(); |
---|
98 | $times[ $table ][ $engine ][ $percentage ]['yes'] = array(); |
---|
99 | $times[ $table ][ $engine ][ $percentage ]['no'] = array(); |
---|
100 | |
---|
101 | for ( $ii = 0; $ii < 5; $ii++ ) { |
---|
102 | $start = microtime( true ); |
---|
103 | $res = mysql_query( "SELECT * FROM {$table}_{$engine}_{$percentage} WHERE c='yes';" ); |
---|
104 | $end = microtime( true ); |
---|
105 | |
---|
106 | mysql_free_result( $res ); |
---|
107 | |
---|
108 | // Ignore the first result, it was taken while the buffer pool was warming up |
---|
109 | if ( $ii ) { |
---|
110 | $times[ $table ][ $engine ][ $percentage ]['yes'][] = $end - $start; |
---|
111 | } |
---|
112 | |
---|
113 | $start = microtime( true ); |
---|
114 | $res = mysql_query( "SELECT * FROM {$table}_{$engine}_{$percentage} WHERE c='no';" ); |
---|
115 | $end = microtime( true ); |
---|
116 | |
---|
117 | mysql_free_result( $res ); |
---|
118 | |
---|
119 | if ( $ii ) { |
---|
120 | $times[ $table ][ $engine ][ $percentage ]['no'][] = $end - $start; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | $avg = array_sum( $times[ $table ][ $engine ][ $percentage ]['yes'] ) / count( $times[ $table ][ $engine ][ $percentage ]['yes'] ); |
---|
125 | echo "{$table}_{$engine}_{$percentage} ('yes'): $avg\n"; |
---|
126 | |
---|
127 | $avg = array_sum( $times[ $table ][ $engine ][ $percentage ]['no'] ) / count( $times[ $table ][ $engine ][ $percentage ]['no'] ); |
---|
128 | echo "{$table}_{$engine}_{$percentage} ('no'): $avg\n"; |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | $times_indexed = array(); |
---|
134 | foreach ( $tables as $table => $row ) { |
---|
135 | $times_indexed[ $table ] = array(); |
---|
136 | foreach ( $engines as $engine ) { |
---|
137 | $times_indexed[ $table ][ $engine ] = array(); |
---|
138 | foreach( $percentages as $percentage ) { |
---|
139 | $times_indexed[ $table ][ $engine ][ $percentage ] = array(); |
---|
140 | $times_indexed[ $table ][ $engine ][ $percentage ]['yes'] = array(); |
---|
141 | $times_indexed[ $table ][ $engine ][ $percentage ]['no'] = array(); |
---|
142 | |
---|
143 | mysql_query( "ALTER TABLE {$table}_{$engine}_{$percentage} ADD INDEX (c);" ); |
---|
144 | |
---|
145 | for ( $ii = 0; $ii < 5; $ii++ ) { |
---|
146 | $start = microtime( true ); |
---|
147 | $res = mysql_query( "SELECT * FROM {$table}_{$engine}_{$percentage} WHERE c='yes';" ); |
---|
148 | $end = microtime( true ); |
---|
149 | |
---|
150 | mysql_free_result( $res ); |
---|
151 | |
---|
152 | if ( $ii ) { |
---|
153 | $times_indexed[ $table ][ $engine ][ $percentage ]['yes'][] = $end - $start; |
---|
154 | } |
---|
155 | |
---|
156 | $start = microtime( true ); |
---|
157 | $res = mysql_query( "SELECT * FROM {$table}_{$engine}_{$percentage} WHERE c='no';" ); |
---|
158 | $end = microtime( true ); |
---|
159 | |
---|
160 | mysql_free_result( $res ); |
---|
161 | |
---|
162 | if ( $ii ) { |
---|
163 | $times_indexed[ $table ][ $engine ][ $percentage ]['no'][] = $end - $start; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | $avg = array_sum( $times_indexed[ $table ][ $engine ][ $percentage ]['yes'] ) / count( $times_indexed[ $table ][ $engine ][ $percentage ]['yes'] ); |
---|
168 | echo "{$table}_{$engine}_{$percentage} indexed ('yes'): $avg"; |
---|
169 | |
---|
170 | if ( $avg > ( array_sum( $times[ $table ][ $engine ][ $percentage ]['yes'] ) / count( $times[ $table ][ $engine ][ $percentage ]['yes'] ) ) ) { |
---|
171 | echo " (slower)\n"; |
---|
172 | } else { |
---|
173 | echo " (faster)\n"; |
---|
174 | } |
---|
175 | |
---|
176 | $avg = array_sum( $times_indexed[ $table ][ $engine ][ $percentage ]['no'] ) / count( $times_indexed[ $table ][ $engine ][ $percentage ]['no'] ); |
---|
177 | echo "{$table}_{$engine}_{$percentage} indexed ('no'): $avg"; |
---|
178 | |
---|
179 | if ( $avg > ( array_sum( $times[ $table ][ $engine ][ $percentage ]['no'] ) / count( $times[ $table ][ $engine ][ $percentage ]['no'] ) ) ) { |
---|
180 | echo " (slower)\n"; |
---|
181 | } else { |
---|
182 | echo " (faster)\n"; |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|