All pastes #944357 Raw Edit

Untitled

public text v1 · immutable
·published 2008-03-16 01:20 UTC
rendered paste body
mysql> create table t (a char(10));
Query OK, 0 rows affected (0.03 sec)

mysql> alter table t add index first_char (a(1));
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into t values ('0ab'),('123'),('abc'),('def');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into t values ('999'),('9z'),('z9');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t;
+------+
| a    |
+------+
| 0ab  |
| 123  |
| abc  |
| def  |
| 999  |
| 9z   |
| z9   |
+------+
7 rows in set (0.00 sec)

mysql> select * from t where a > '/' and a < ':';
+------+
| a    |
+------+
| 0ab  |
| 123  |
| 999  |
| 9z   |
+------+
4 rows in set (0.00 sec)

mysql> explain select * from t where a > '/' and a < ':';
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key        | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
|  1 | SIMPLE      | t     | range | first_char    | first_char | 2       | NULL |    3 | Using where |
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> insert into t values ('/bad');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t where a > '/' and a < ':';
+------+
| a    |
+------+
| 0ab  |
| 123  |
| 999  |
| 9z   |
+------+
4 rows in set (0.00 sec)

mysql> select * from t where a between '/' and  ':';
+------+
| a    |
+------+
| 0ab  |
| 123  |
| 999  |
| 9z   |
| /bad |
+------+
5 rows in set (0.00 sec)

mysql> alter table t drop index first_char;
Query OK, 8 rows affected (0.02 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql> select * from t where a > '/' and a < ':';
+------+
| a    |
+------+
| 0ab  |
| 123  |
| 999  |
| 9z   |
| /bad |
+------+
5 rows in set (0.00 sec)