0

I have a view which uses many tables, and statements and functions. It is very slow, the main reason being that I only have read-only access to the main tables which do not have the appropriate indexes, so joins evaluate very slowly.

Following some advice I have broken the joins down using temporary tables, to ensure that every join has indexed fields on both sides. This has worked very well. If I run a select to pull the final output it runs 60x quicker than previously. However I cannot use this to drive the necessary view, as views cannot refer to temporary tables.

Are there any alternatives to temporary tables, or different approaches I could take?

Thanks

1
  • 2
    Can't you convince whoever administers the database to add appropriate indexes? Joining between tables normally happens via key columns, so I would expect that indexes should be there regardless of your needs. Commented Dec 29, 2017 at 11:06

2 Answers 2

0

View is just an alias to the query that allow you to use its result in other queries in terms of tables not subqueries. But each time you refer to the view, the query from its definition is invoked. No temporary tables created when view is defined.

Therefore views do not improve the overall performance at all and are intended to make DB structure more clear and logical.

Prior to the MySQL 5.7 query results can be cached that really speed up some queries. But as far as caching conflicts with InnoDB internals, it is disabled now and can cause significant slowdown and overall performance losses when enabled. So views should be considered as threat to the performance.

On the other hand, temporary tables, especially those with ENGINE=MEMORY, can significantly improve the performance. But you have to ensure that temptable is updated each time the referred tables are updated. Triggers are the good tool to keep reference consistency of the derived tables. Sure even memory-based temptables should be properly indexed for maximum performance.

1
  • Keep in mind that MEMORY engine does not supports text and blob fields and for maximum performance memory-based temporary tables should have fixed-length rows.
    – Kondybas
    Commented Jul 14, 2017 at 13:34
0

Write a stored procedure to do the task. It can end with (approximately) the SELECT that 'should be' in the VIEW.

2
  • Hi Rick, the view is used in an Access database via an ODBC connection. Can a stored procedure be used for this? Thanks
    – agwah
    Commented Jul 14, 2017 at 12:54
  • (I can't help you with Access VIEWs, only MySQL.)
    – Rick James
    Commented Jul 14, 2017 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.