You should look for all the conditions for each connection ... provided. They work the same way.
Suppose we write
select name from customer where customerid=37;
Somehow, the DBMS must find a record or records with customerid = 37. If there is no index, the only way to do this is to read each record in the table by comparing customerid with 37. Even when it finds it, it has no way to find out that there is only one therefore she must continue to search for others.
If you create an index for customerid, the DBMS has ways to find the index very quickly. This is not a sequential search, but, depending on the database, a binary search or some other efficient method. Just as it does not matter, you must admit that it is much faster than sequential. Then the index translates it directly into the corresponding record or entries. In addition, if you indicate that the index is "unique", then the database knows that there can only be one, so it does not waste time searching for the second. (And the DBMS won't let you add a second.)
Now consider this query:
select name from customer where city='Albany' and state='NY';
Now we have two conditions. If you have an index for only one of these fields, the DBMS will use this index to search for a subset of records, and then search them sequentially. For example, if you have a state index, the DBMS will quickly find the first record for NY, then sequentially search for the city = "Albany" and stop looking when it reaches the last record for New York.
If you have an index that includes both fields, that is, "create an index for the client (state, city)," then the DBMS can immediately increase to the required records.
If you have two separate indexes, one in each field, the DBMS will have different rules that it applies to determine which index to use. Again, exactly how this is done depends on the specific DBMS that you use, but basically it tries to keep statistics on the total number of records, the number of different values and the distribution of values. Then he will sequentially search for these records for those that satisfy another condition. In this case, the DBMS is likely to observe that there are far more cities than there are states, therefore, using the city index, it can quickly approach the Albany records. He will then search them sequentially, checking the status of each against NY. If you have entries for Albany, California, they will be skipped.
Each connection requires some kind of search.
Let's say we write
select customer.name from transaction join customer on transaction.customerid=customer.customerid where transaction.transactiondate='2010-07-04' and customer.type='Q';
Now the DBMS should decide which table to read first, select the necessary records, and then find the corresponding records in another table.
If you have an index for transaction.transactiondate and customer.customerid, the best plan is probably to search for all transactions with this date, and then for each of them find the client with the appropriate client, and then make sure the client is of the correct type.
If you don’t have an index on customer.customerid, then the DBMS can quickly find the transaction, but then for each transaction it will have to sequentially search for a client table that is looking for a suitable client. (This is likely to be very slow.)
Suppose that only those indexes that you have are on transaction.customerid and customer.type. Then the DBMS is likely to use a completely different plan. He probably scans the client table for all clients with the correct type, and then for each of them they find all the transactions for that client and look through them sequentially on the desired date.
The most important key to optimizing is figuring out which indexes will really help and create those indexes. Additional, unused indexes are a burden on the database because they require work to work, and if they are never used, it will be wasted.
You can specify which DBMS indexes will be used for any given query using the EXPLAIN command. I use this all the time to determine if my queries are optimized or if I will create additional indexes. (Read the documentation for this command to explain its output.)
Warning. Remember that I said that the DBMS stores statistics on the number of records and the number of different values, etc. in each table. EXPLAIN can give you a completely different plan today than yesterday if the data has changed. For example, if you have a query that joins two tables, and one of these tables is very small and the other large, it will be biased to read the first table and then search for matching records in the large table. Adding records to the table can change, which is larger, and thus lead the DBMS to change its plan. Therefore, you should try to use EXPLAINS for a database with realistic data. Working with a test database with 5 records in each table is much less valuable than working with a live database.
Well, much more can be said, but I do not want to write a book here.