# Hilfspalte:
ALTER TABLE gn250 ADD COLUMN is_pilsdorf TINYINT;
### Query OK, 0 rows affected (0.008 sec)
### Records: 0 Duplicates: 0 Warnings: 0
# Füllen:
UPDATE gn250 SET is_pilsdorf=1 WHERE Name="Pilsdorf";
### Query OK, 1 row affected (0.006 sec)
### Rows matched: 1 Changed: 1 Warnings: 0
# Indexieren:
CREATE INDEX is_pilsdorf ON gn250 (is_pilsdorf);
### Query OK, 0 rows affected (0.190 sec)
### Records: 0 Duplicates: 0 Warnings: 0
Abfrage:
SELECT NNID, Name FROM gn250 ORDER BY is_pilsdorf,Name
### (gewünschtes Ergebnis)
Aber: „Rakete geht irgendwie anders“:
149745 rows in set (0.565 sec)
Nachgeschaut:
Die Tabelle hat einen Index auf Name, einen auf is_pilsdorf (und weitere).
Aber:
explain SELECT NNID, Name FROM gn250 ORDER BY is_pilsdorf,Name;
+------+-------------+-------+------+---------------+------+---------+------+--------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+------+---------+------+--------+----------------+
| 1 | SIMPLE | gn250 | ALL | NULL | NULL | NULL | NULL | 141636 | Using filesort |
+------+-------------+-------+------+---------------+------+---------+------+--------+----------------+
Demnach wird also der Index nicht benutzt. Selbst wenn ich dessen Benutzung erzwingen will …
SELECT NNID, Name FROM gn250 USE INDEX (Name) USE INDEX (is_pilsdorf) ORDER BY is_pilsdorf,Name;
… wird das nicht anders.
Ist die Tabelle mit fast 150000 Zeilen etwa zu klein? Oder hab ich was falsch gemacht?
MariaDB [test]> describe gn250;
+-------------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------+------+-----+---------+-------+
| NNID | varchar(16) | YES | UNI | NULL | |
| DATUM | date | YES | MUL | NULL | |
| OBA | varchar(100) | YES | MUL | NULL | |
| OBA_WERT | varchar(100) | YES | MUL | NULL | |
| NAME | varchar(255) | YES | MUL | NULL | |
| SPRACHE | varchar(100) | YES | | NULL | |
| GENUS | varchar(100) | YES | | NULL | |
| NAME2 | varchar(100) | YES | MUL | NULL | |
| SPRACHE2 | varchar(100) | YES | | NULL | |
| GENUS2 | varchar(100) | YES | | NULL | |
| ZUSATZ | varchar(100) | YES | | NULL | |
| AGS | bigint(20) | YES | | NULL | |
| RS | bigint(20) | YES | | NULL | |
| HOEHE | int(11) | YES | | NULL | |
| HOEHE_GER | int(11) | YES | | NULL | |
| EWZ | int(11) | YES | | NULL | |
| EWZ_GER | int(11) | YES | | NULL | |
| GEWK | varchar(100) | YES | | NULL | |
| GEMTEIL | tinyint(1) | YES | | NULL | |
| VIRTUELL | tinyint(1) | YES | | NULL | |
| GEMEINDE | varchar(100) | YES | MUL | NULL | |
| VERWGEM | varchar(100) | YES | MUL | NULL | |
| KREIS | varchar(100) | YES | MUL | NULL | |
| REGBEZIRK | varchar(100) | YES | MUL | NULL | |
| BUNDESLAND | varchar(100) | YES | MUL | NULL | |
| STAAT | varchar(3) | YES | MUL | NULL | |
| GEOLA | decimal(7,4) | YES | MUL | NULL | |
| GEOBR | decimal(7,4) | YES | MUL | NULL | |
| GKRE | decimal(20,10) | YES | | NULL | |
| GKHO | decimal(20,10) | YES | | NULL | |
| UTMRE | decimal(20,10) | YES | | NULL | |
| UTMHO | decimal(20,10) | YES | | NULL | |
| BOX_GEO | text | YES | | NULL | |
| BOX_GK | text | YES | | NULL | |
| BOX_UTM | text | YES | | NULL | |
| is_pilsdorf | tinyint(4) | YES | MUL | NULL | |
+-------------+----------------+------+-----+---------+-------+