views:

74

answers:

1

I create a view with many joins. I want to group the resultset by some of the fields I selected.

Does setting indices on these fields increases the performance of the query? I'm using MS SQL Server, but it should be nearly the same for all dbs, shouldn't it?

SELECT table1.field1, table1.field2, table2.field1
FROM table1
INNER JOIN table2 ON table1.primkey = table2.tablekey
GROUP BY table1.field1, table1.field2, table2.field1
+1  A: 

If you run a query with "Show Execution Plan" switched on, you can ask MS SQL server to suggest indices. It will indicate potential optimising indices to you by creating a hypothetical index on a column it thinks is holding things up and comparing that against your query.

Sohnee
This is an outstanding suggestion of lasting value to novices and veterans alike. It will work better once the database contains a non-trivial quantity of data. Be sure to update your database statistics using dbcc prior to using this advice.
Peter Wone