I have an Access 2003 DB and I want to improve its performance. Yesterday, I read an article about Execution Plan (Show Plan) and today I ran my show Plan for this query:
SELECT tb_bauteile_Basis.*
FROM tb_bauteile_Basis
ORDER BY tb_bauteile_Basis.Name;
I put an index on the Name field, and show its query plan:
Inputs to Query -
Table 'tb_bauteile_Basis'
Using index 'Name'
Having Indexes:
Name 1553 entries, 17 pages, 1543 values
which has 1 column, fixed
ID 1553 entries, 4 pages, 1553 values
which has 1 column, fixed, clustered and/or counter
- End inputs to Query -
01) Scan table 'tb_bauteile_Basis'
Using index 'Name'
Next, I deleted the index from Name, and the new query plan is:
- Inputs to Query -
Table 'tb_bauteile_Basis'
Using index 'PrimaryKey'
Having Indexes:
PrimaryKey 1553 entries, 4 pages, 1553 values
which has 1 column, fixed, unique, clustered and/or counter, primary-key, no-nulls
Plauskomponente 1553 entries, 4 pages, 3 values
which has 1 column, fixed
Name 1553 entries, 17 pages, 1543 values
which has 1 column, fixed
ID 1553 entries, 4 pages, 1553 values
which has 1 column, fixed, clustered and/or counter
- End inputs to Query -
01) Scan table 'tb_bauteile_Basis'
Using index 'PrimaryKey'
How should I interpret these two query plans?
In the second Showplan that means, should I create an index for Plauskomponente,Name,ID, and should I make a composite index for these three fields? How can I determine whether I should make a composite index?
And why doesn’t Plauskommponente appear in the first showplan?
in the second Showplan that means: I should put Index for Plauskomponente, Name, ID? and should I make a Composite Index From these three fields? How can I find that I should make a Composite index?
Those names appear in the ShowPlan section which shows the information Jet analyses when devising the query plan. Indexes on those 3 fields, either separate indexes or a single composite index based on all three, would not help that particular query. And actually adding indexes will slow down other operations … when you add or delete rows, or edit values in indexed fields, the db engine must write changes to the table and to the composite index.
Optimizing indexing can be tricky. Indexes can speed up SELECT, but slow down INSERT, DELETE and UPDATE operations. You need to find the right balance for your application. If you’re relatively new at this in Access, try the Performance Analyzer wizard from the database tools section of the menu. Examine the suggestions it offers. Many times those suggestions will involve indexes. You can add indexes it suggests, then drop them later if they degrade or don’t improve your overall performance.
Why the Plauskommponente doesn’t appear in the first showplan?
Beats me. I would guess the query planner already found the Names index, so considered it pointless to look for other indexes.
However, that brings up another important point. The table includes 1,553 rows, but Plauskomponente contains only 3 different values. With such low variability, an index on Plauskomponente would probably not even be used in a plan for a query which had a WHERE clause based on Plauskomponente. @Namphibian touched on the reason in a comment. Reading the index to find out which rows match the criterion, then reading the matching rows could be considered more costly than just ignoring the index and reading all the rows from the table.
Finally notice the statistics mentioned in the ShowPlan information sections. Those statistics are updated when you compact the database. So compacting is useful to give the query planner the latest information to use as it makes its decisions about how to optimize the query plan.