I think you're missing the point of primary keys and indexes. Let me briefly explain.
A primary key is a unique identifier for a single row, often used in other tables to reference that row. For example, if you had a room table and a reservation table, then the reservation table would refer to the room's primary key so that you could then get the room details while you get the reservation details by using an INNER JOIN.
An index is a column which has been treated specially by the DB engine so that information can be fetched more rapidly. For example, you'll want to quickly find the dates a room is reserved so that others do not reserve the same date, so indexing the reservation date column would speed up that query.
Let's assume that you have a database structure like this:
Code:
room
room_number UNSIGNED SMALLINT (that gives up to 65535 rooms [room number > 0])
beds UNSIGNED TINYTINT
reservation
room_number (room's primary key)
reservation_date
customer_id UNSIGNED MEDIUMINT (assume a customer table exists and this is the primary key)
Now, we may want to fetch all reservations for a specific date. This query would do that:
Code:
SELECT * FROM reservation rv, room r, customer c WHERE rv.room_number=r.room_number AND r.customer_id=c.customer_id AND reservation_date='2008-12-08'
Notice how that links the tables together by their primary key? In each table which uses the primary key, you'll want to have the foreign key (the primary key from another table) indexed as well so that the database engine doesn't have to go searching through all 40,000 customers to find the one that matches or all 200 rooms to find the matching room -- instead, it uses the index to rapidly know to skip the first 25,376 customers and get to the correct one and skip the first 124 rooms to find the matching room.
Next, notice that the reservation table has 2 fields which create a unique combination: room_number and reservation_date. Creating a compound primary key of these 2 will help you speed things up. BUT,
order matters. Going back to our query, we need to ask ourselves a question: Which order of the compound key will reduce the database engine's overall work. If we look at room_number and then reservation_date, then what would the database engine have to do? Well, it'd need to search through room_numbers before searching through reservation_dates to find the correct row. Since the room_number is not provided, this results in unnecessary searching. The reservation_date, however, is present in the query, so
this query would run faster with the date first. However, if we have other queries in our system (VERY likely), then that convenience may not continue to hold true. Take, for instance, this query which tries to find the reservation status for a specific room:
Code:
SELECT * FROM reservation rv, room r, customer c WHERE rv.room_number=r.room_number AND r.customer_id=c.customer_id AND rv.room_number=3
The logic for that query is exactly the opposite of the first query! Now what? A compound key is not the best solution in this case! Instead, index each column separately and create a new primary key that is unique to each reservation. For example reservation_id AUTO_INCREMENT UNSIGNED INT (assuming a lot of reservations!). By making it auto-increment, it will do all the +1 stuff for you automatically. By having the separate indexes on columns, your 2 queries are then optimized.
I said "brief", but have gone on for awhile and not even performed a complete analysis, but I hope I've demonstrated that before designing your database and implementing your code, you first need to ask yourself about the underlying system and the questions it will ask of the database under typical usage. Once you know how the system will be used, you'll be better able to design a structure which will be optimized for speed. Caveat: Indexes take up additional space, so keep that in mind too -- it'd be a
really bad idea to index every column.