View or temporary table?

6

Looking at 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've been searching and found this question that deals specifically with views . I've been reading the answer marked as accepted and I understood more about what they are and what "visions" are for ", but in the last line my question was exposed.

  

I'm glad you did not ask the difference to use a temporary table

In ) Fortunately this is my point !!

  • What sets a view of a temporary table ?
  • Is there a "formula" to find out when to use one or the other?
asked by anonymous 10.09.2018 / 18:41

2 answers

7

The jocular text there is just 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

The view , as can be seen there is not a real table (unless it is materialized), it is just a way of consulting something in a simpler way, possibly already doing a denormalization and offering a way to access certain data without entering into 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 on it. It is always available for inquiries, until someone has it removed. Some implementations allow you to change your data by reflecting on the original table (s). The cost only exists in the normal query rendering, which is virtually the same as doing the query entered in the view template manually.

To complete the materialized view creates the physical table, but always based on existing table (s). It may look more like the temporary table, but it always (potentially) generates different results in each result, and the table is always available to everyone with due privilege until its removal. There is cost of space and processing, not even using it directly. I've seen cases where it can be updated independently and can create scenarios, and then it returns to the original state, but I do not know if it was made for it.

Temporary table

The temporary table is physical and only exists when you have it created. It's usually based on other tables, but it may just be something coming externally (technically, the views can also, but it makes less sense). If nothing is changed in it all applied query will generate the same result. To change your data as well as your creation, you need an explicit code to do so. Usually it is available for that session, but there is a way to make it more "permanent", even if it does not make sense. It is used on demand and has cost of space and processing as needed.

As the name says it serves for temporary purposes, something you do not want to keep in the system, and may even do something unrelated to the rest of the base, although it makes little sense. It compares a little more to the materialized view 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 , it almost always costs too much to make up.

When you compare simple view with the temporary table, it is easy to establish where it should be used. view does not allow you to move it. It is a simplification and optimization mechanism and is preferable whenever this fits.

Since the temporary table has more costs and implications, then you always need more justification, you have to make sure that another solution does not solve. It should almost never be used as a view . It should be used to create new data, prepare data for use in the base, create scenarios. You do what you understand right then, you can experiment without fear, it does not do mars of your model. Treat it like a sketch. Think of it as a workout, a preparation for something, a simulation, something independent of the base.

It may be necessary in very complex queries that depend on some manipulation in the data before they are 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 the DBA does. Those who do not do "normal" queries and processing make less sense, but can do it, especially in very complex reports.

If your SGDB thinks you should, you can create a temporary table to handle the view , but this is something transparent to you.

    
10.09.2018 / 19:14
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 later. When disconnecting from the server, temporary tables are automatically discarded.

When it is useful to use temporary tables in MySQL

Some operations require some information to be short - and they are removed when they are no longer needed. The removal part can be done automatically by MySQL. You do not have to remember to delete a table, which is no longer useful, with the command DROP TABLE .

To do this, simply create a table, with the TEMPORARY option, like this:

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 statements, returning a given view of data from one or more tables. These objects are sometimes called " in> virtual tables ", formed from other tables that in turn are called" based tables "or other Views. And in some cases, Views are upgradeable and can be target statements of INSERT, UPDATE e DELETE , which actually modify your " based tables ".

The benefits of using Views (other than those already noted)

A View can be used, for example, to return a value based on a record identifier; It can be used to promote data constraints to increase data security and define table-and-column access policies. They can be configured to display different columns for different users of the database; It can be used with a set of tables that can be joined to other sets of tables using JOIN’s or UNION .

List views

SELECT * FROM INFORMATION_SCHEMA.VIEWS

References and links

Creating temporary tables in MySQL

MYSQL - Working with Views

List all Stored Procedures, Tables and Views SQL Server

MySQL and temporary tables

    
10.09.2018 / 19:13