Hi I have a table that is like this :
create table nasd_snapshot
( minute_id smallint not null
, symbol_id integer not null
...
PRIMARY KEY ( symbol_id, minute_id )
Does it matter, or would it make it more efficient, to have the columns in the PRIMARY KEY in the same order as the columns in the table?
I guess I don’t know enough about how things are stored internally, but have done some research. Any suggestions or links to some articles on this appreciated.
THX!
DON
Adding an index ALREADY sorts them. The point of adding an index is to have the data sorted so mysql can fetch the data really fast. It already has the index columns sorted so you do not need to worry about that anymore 🙂 However, as you can imagine, storing data + sorted data takes more space in the server. So do not overdo with indexing. One more thing – PRIMARY index is the same as normal index except it also says that the data in that index’s columns will be unique for the table. The best example to try it yourself would be: put some data into your database, lets say 10.000 records. Let them be, for example,
(id, name). Try the query something likeSELECT * FROM table WHERE id=6451. Then add the index (or PRIMARY KEY) on id column. Try again. If you are using PhpMyAdmin, it shows how much time the query took to execute. You will see HUGE difference between the indexed and unindexed cases. Also, take a peek at how much space the table takes.