mig <
meu...@gmail.com> wrote:
> On Oct 26, 5:28=A0pm, Axel Schwenke <
axel.schwe...@gmx.de> wrote:
>>
>> I wonder what you try to achieve.
>> Or what you think indexes are good for.
> I have two ascii fields in the same table , I
> want this indexed result
>
> field A field B
> ==== ====
>
> zzzz aaaa
> zzzz bbbb
> zzzz cccc
> yyyy aaaa
> yyyy bbbb
> yyyy cccc
> xxxx aaaa
> xxxx bbbb
> xxxx cccc
>
> "order by" works fine, but I need replace it because performance
> problems (lot of tmp tables), I need to build a true index to perform
> this task.
It seems the misunderstanding is here. There are many reasons
why a query could be slow. Sorting the result is not a typical
reason. Accessing rows in index order (to save the external sort)
is not necessarily better than a table scan followed by sorting.
The reason is random I/O vs. streaming I/O.
What you are doing here, is called "premature optimization".
A good indexing strategy is what I said in my last post:
1. start with a normalized schema; the only indexes should be
those to enforce constraints
- primary keys
- unique constraints
- foreign key constraints
2. look at your queries and how long they take to execute.
The slow query log could be helpful here.
3. if there are no (too) slow queries or if the slow queries
are running only occasionally, then stop.
Probably there will be slow queries. You can now start to optimize
the schema (and/or the queries). Start with the query that runs most
often. Then do:
A. run the query through EXPLAIN and understand(!) the result
B. add indexes to the tables accessed by the query and/or
rewrite the query such that it can (better) use indexes
C. verify that the problem query is really faster now
Probably this will have effect on other queries too. So go back
to 2. and repeat.
In step B. there are many additional possibilities. To name a few:
- add index hints to queries
- rewrite subqueries as joins
- add redundant data (denormalize the schema)
- replace views or subqueries by properly indexed temporary tables
HTH, XL