Domanda

So does adding indexes to tables that are in a view give you any performance boost. Let's say we have following tables:

CAR

  • ID (pk)
  • model
  • owner

TYRES

  • ID (pk)
  • carID
  • size

CAR_DETAILS (view)

  • carID
  • owner
  • model
  • tyreSize

View is joined on CAR.ID=TYRES.CarID. Now let's say following query is executed: SELECT model, tyreSize FROM CAR_DETAILS WHERE tyreSize=19; Does indexing size on TYRES benefit you if you are querying the view on indexed field?

È stato utile?

Soluzione

Yes, indexes on constituent tables can be used in queries on views. Views usually use the MERGE algorithm which basically merges the query on the view with the query that defines the view. You can think of it like this:

The view query:

SELECT * FROM CAR JOIN TYRES ON (CAR.ID=TYRES.CarID)

Query on view:

SELECT model, tyreSize FROM CAR_DETAILS WHERE tyreSize=19

Merged query:

SELECT model, tyreSize FROM CAR JOIN TYRES ON (CAR.ID=TYRES.CarID) WHERE tyreSize=19

The query isn't really executed like that but it gives you an idea of how it will be run. In your case, an index on tyreSize will probably be used if it exists. Depending on your other queries, an index on TYRES.CarID will also probably be helpful.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top