View or temporary table?

Asked

Viewed 1,944 times

6

Analyzing a code problem I came across different approaches to similar problems where one has a view for data access, and another uses a temporary table.

I searched and found this question which deals specifically with views. I’ve been reading the reply marked as accepted and understood more about what they are and what they are for "visions", but in the last line my doubt was exposed.

Good thing you didn’t ask the difference to use a temporary table

(In)Fortunately this is my point!!

  • What differentiates a view of a temporary table?
  • There is a "formula" to find out when to use one or the other?
  • The temporary table if "auto-excludes" at the end of the connection. The view is not a virtual table, its results are from another (s) table(s).

2 answers

7


The jocose text there is only because there would be many subjects :)

In fact they have nothing to do, they are very different mechanisms for very different purposes, with different commitments.

View

To view, as can be seen there, it is not a table of truth (unless it is materialized), it is just a way of consulting something in a simpler way, possibly already doing a denormalization and offering a form of access to certain data without entering others for a user that you do not want to expose all of the table(s) used in the view. The data is always generated from existing real tables and the specific result is available for this query. Each query in it can generate a different result without doing anything in it. It is available for queries always, until someone has it removed. Some implementations allow you to change your data by reflecting on the original table(s) (ais). The cost only exists in the normal processing of the query, which is virtually the same as the one that made manually the query entered in the view.

To top it off, the materialized view creates the physical table, but always based on an existing table(s). It may look more like the temporary table, but it always (potentially) generates different results on each result and the table is always available to everyone with due privilege until it is removed. There is cost of space and processing, even if not using it directly. I’ve seen cases where it can be upgraded independently by creating scenarios, and then it goes back to its original state, but I don’t know if it’s made for this.

Temporary table

The temporary table is physical and only exists when you have created. It is usually based on other tables(s), but it may just be something coming externally (technically as views also can, but it makes less sense). If nothing is changed in it every query applied will generate the same result. To change your data, as in your creation, you need an explicit code that you have done. Normally it is available for that session, but there is a way to make it more "permanent", even if it doesn’t make sense. It is used on demand and costs space and processing as required.

As the name says, it serves temporary purposes, something you don’t want to keep in the system, and it can even do something little related to the rest of the base, although it makes little sense. It compares a little more to the materialized vision because both are physical. But the temporary ones are for extra operations apart from the normal operation of the base, the materialized ones are optimizations to the normal access of the base.

When to use each

It’s rare to need materialized view, almost always it brings too much costs to compensate.

When compared to view simple with the temporary table it is easy to establish where it should be used. A view does not allow to touch it. It is a mechanism of simplification and optimization and is preferable whenever this fits.

The temporary table has more costs and implications, so you always need more justification, you have to be sure that another solution does not solve. Practically never to be used as a view. It should be used to create new data, prepare data for use in the database, create scenarios. You do what you want at that moment, you can experiment without fear, it does not make mars of your model. Treat it as a draft. Think of it as a training, a preparation for something, a simulation, something independent of the base.

It may be needed in very complex queries that depend on some manipulation in the data before it is used.

The temporary table is a normal table and can do everything with it, including creating indexes.

Dbas tend to use this more than developers. It makes more sense in procedures or complex processing that only DBA does. Who does not make queries and more "normal" processing makes less sense, but can do, especially in very complex reports.

If your SGDB thinks it should it can create a temporary table to better manipulate the view, but that’s up to him, something transparent to you.

Temporary table may never go to disk.

  • 1

    Because writing in your own words is always better... +1

  • Very good the explanation, clarified what he had of doubt!!

4

Temporary tables

Using a temporary table in Mysql, allows you to perform tests or services on a transient entity, without worrying about cleaning up the mess afterwards. When disconnecting from the server, the temporary tables are automatically discarded.

When it is useful to use temporary tables in Mysql

Some operations require that some information be short-lived - and that it be removed when it is no longer necessary. The removal part can be done automatically by Mysql. You don’t need to remember to delete a table, which is no longer useful, with the command DROP TABLE.

To do this, just create a table, with the option TEMPORARY, thus:

CREATE TABLE TEMPORARY Nome_da_tabela

List temporary tables

SELECT * FROM INFORMATION_SCHEMA.INNODB_TEMP_TABLE_INFO\G


Views

A View is an object that belongs to a database, defined based on SELECT’s declarations, returning a certain data view from one or more tables. These objects are sometimes called "virtual Tables", formed from other tables which in turn are called "Tbased" or even other Views. And some cases, Views are upgradable and can be declared targets INSERT, UPDATE e DELETE, who actually modify their "Tbased”.

The benefits of using Views (in addition to those already highlighted)

A View can be used, for example, to return a value based on a record identifier; It can be used to promote data restrictions to increase data security and to define access policies at table and column level. Can be configured to show different columns to different database users; Can be used with a set of tables that can be attached to other sets of tables using JOIN’s or UNION.

List views

SELECT * FROM INFORMATION_SCHEMA.VIEWS


References and links

How to create temporary tables in Mysql

MYSQL - Working with Views

List all Stored Procedures, Tables and SQL Server Views

Mysql and temporary tables

Browser other questions tagged

You are not signed in. Login or sign up in order to post.