How expensive is creating a database view? - mysql

Right now, I'm altering a table with a crap ton of data, which is going to take weeks to run. In the meantime, I figured I'd create a new table to write to while this one is locked.
I can create an empty table and just write to this one, and check both tables when reading. Or I can create a copy of the current table and do all reads/writes from this copy for the next month.
It looks like copying the table is not the best solution. What about creating a view (just to read from) combining both tables?
CREATE VIEW MY_TABLE_VIEW AS
SELECT * FROM MY_TABLE
UNION ALL
SELECT * FROM MY_TABLE_COPY
Would creating the view be just as expensive as altering the original table? Should I instead just change all my table reads to UNION ALL the results from both tables?

Creating a view is a metadata-only operation. Views don't store any data. Querying the view will actually query the base table as if you ran the query in the view definition. So there's practically nothing to do to create the view. It only stores the query as metadata.

Related

Is there any way to synchronize view and table in phpmyadmin?

I have a task to do synchronizing view with table in phpmyadmin. As example, I have a view table like this:
VIEW:
TABLE:
the data records of table is same as view. I copy the data from view to table using
CREATE TABLE tablename AS (SELECT * FROM viewname)
I want to synchronize them. As example, when view is updated then table will be updated too. The meaning of view's updating is like editing the view then I import the view again by deleting the old one. Is there any way to do that? I want to synchronize the view and the table because the time took so long when I open the view. So I made table which is same as view so that I'm able to open it faster. Thanks in advance
VIEW in MySQL is a virtual table or logical view of a table as mentioned by Jens that has no data stored on it like a table.
Instead, it fetches the values directly from the table. The values in VIEW changes when the values from the table it fetches changes.
Therefore you will need to sync between the tables and not between view and table.

How to synchronize view table to manual temporary table?

I have tables in database that consist of so many data records. I made view tables because I joined the tables. Then, when I tried to open the view tables, the time took so long because the tables had many data. So, I decided to make temporary table like this:
CREATE TABLE temp_ AS (SELECT * FROM view)
then, I want to synchronize the view and temporary table. As the example, when I tried to update the view table, then we're able to know that the temporary table has to change too because the temporary is synchronized with the view table. Maybe, it can be solved when the view's updated time > generated time than we have to update the table
I have table like this
I manually inserted the data records to both of that table. What may I do to synchronize them? Thanks in advance

MySQL - Automate a view update

I'd like to know if it is possible to create an SQL function that will automatically update my View and add to it new tables in my DB.
My DB consist of multiple tables (same data structure) and are named as follow "MM_DD", now I would like to create a VIEW that joins all this data ( pretty simple , see query below) but I wish to automate the process so every time a new table is added the view will update.
CREATE OR REPLACE VIEW `viewTable` AS
select *,
md5(CONCAT(`columnA`,`columnB`)) AS myPK_id
from `05_06`
union all
select *,
md5(CONCAT(`columnA`,`columnB`)) AS myPK_id
from `05_08`
ect...
What I am doing at the moment is using PHP every time a table is added. It loops through the tables and create / update the view.
select * from information_schema.tables WHERE table_name LIKE '%05%'
Now that I have an array of table names -> create my Query string -> replace view...
Is this possible to do this in SQL?
If so, what is the correct approach?
Write a stored procedure to rebuild your view. You'll need to use what MySQL internally calls "prepared statements" so you can use string manipulation.
You'll still use your SELECT ... FROM information_schema.tables query to drive this stored procedure.
Once you get this working, you can embed it in a mySQL event, and arrange to run it automatically. For example, you could run it at the same time late at night.
Or, you can invoke the stored procedure immediately after you create one of these tables.

Performance between CREATE TEMP TABLE AS vs INSERT INTO SELECT

I was wondering is there a performance difference between:
query 1: CREATE TEMPORARY TABLE temp_table1 AS SELECT * FROM lookup_table JOIN ...
then
INSERT INTO dest_table SELECT * FROM temp_table1
vs
query 2: INSERT INTO dest_table SELECT * FROM lookup_table JOIN ...
My concern was, the lookup_table is accessed very often by different users and when I run query 2, most of the users need to wait longer to be able to retrieve their result. What I was thinking was to write the data into a temporary table then write it to dest_table afterwards . Im just not sure if writing into a temp table with give a difference performance compared to writing it directly to the destination table. Im using mysql 5.6.
The reason why I need to write data from lookup_table to dest_table is because I need to create a report from it. Seeing how complex the query from lookup_table is makes it very difficult to create a report so I decided to move those data to a single table then just make a report from it.
You're concerned about the lockout time that's taken by the SELECT query that populates this temporary table.
The tables are implemented the same way, so the cost of creating will be very close to the same in either case.
You might be able to get it to go a little faster by creating your temporary table in the MEMORY access method, but I suspect the difference will be minimal; the work involved here is the SELECT / JOIN stuff.
You might be able to get it to go faster by making sure your target table has no indexes when you create it. CREATE ... AS SELECT will do that.
You will be able to make it cheaper to create by getting rid of SELECT * (which yields redundant columns on JOINs anywhow), and instead specify the columns you really need.
But, your best bet is to figure out why you're creating this table, and see if you can deliver on those requirements by writing queries against the source tables instead. If you make those query operations efficient, you've saved yourself lots of data shuffling.

mysql create table like to query?

Is it possible to use the create table query (create table t2 like t1) to get the query instead the actual table? Kind of like duplicating some management consoles Send To editor feature?
I think you're looking for SHOW CREATE TABLE tablename;
'tablename' can be the table within the current database, if you've selected one, or you can qualify it as in 'dbname.tablename'. The output can be used to create the table again. In fact, I sometimes use it in scripts that need to either use an existing table or create it if it doesn't exist. First I'll get the table creation info as shown above, and then I'll use something like"
CREATE TABLE IF NOT EXISTS tablename .........
Where '..........' is all the goodies that SHOW CREATE TABLE tablename gives me.
If you're interested in a good tutorial on MySQL (the database), as well as SQL (the language), you might have a look at the O'Reilly book, Learning MySQL. It's pretty good.