Is posible set a table to multiples schemas on mysql? For example:
i have two schemas with identical tables:
schema1.user
schema2.user
it is possible that when querying schema1.user the information returns the records of schema1.user and schema2.user, without triggers, stored procedures or views?
Short answer to your question: No. You can not do that without triggers, stored procedures or views
A better way to avoid duplicating your data in every database is to query the user table in schema-qualified format.
In other words, even if your default database is schema2 during a given query, you can query the table from schema1:
SELECT ... FROM sometable JOIN schema1.user ON ...
You can mix qualified and non-qualified syntax in the same query. Any table that doesn't have a schema qualifier is assumed to be in the default schema.
See https://dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html
Related
I have a use case where I have a customer table which joins with a couple of other tables. It’s a many to one relationship so joining with these two tables will result in over 100+ rows for each customer. The SELECT statement with over 10000 customers doesn’t perform well.
I was wondering if there is a way to loop over each customer, append the result of the SELECT statement and then provide the output in a MySQL stored procedure. This can be done using a CURSOR and a temporary table but is there a way to do it without a temporary table?
I've a list of records (in excel) which I want to lookup in SQL table to find their entry date in table.
For example I've name of 200 customers in an excel sheet which are also in my SQL table but there are many others as well. I want to compare those users in table and find their date of joining the table i.e the date they were added to the table.
Do you have any column such as "customer_id"? If not, create it in the tables as it will make it easier for you to select and join tables. You can use the alter table statement to add the new column. After adding the columns, join the tables and use the select statement to find the required record.
You must have a key to bind those data. Any other way can be dangerous linking these informations. Maybe, the way is adapt your application or excel to provide this bind between the data.
I have a bunch of tables in my "stats" database.
tcl20151w1d1
tcl20151w1d2
tcl20151w2d1
tcl20151w2d2
tcl20151w3d1
tcl20151w3d2
tcl20151w4d1
eu20151w1d1
eu20151w1d2
eu20151w2d1
eu20151w2d2
eu20151w3d1
eu20151w3d2
eu20151w4d1
..
How can i select all tables that starts with "tcl" in "stats" database. Is it possible? Do I have to union them manually?
You can query information_schema.tables table to get a list of tables where the table name start with tcl.
You can use the list to dynamically create a union query in a stored procedure using string concatenation and prepared statements.
If those tables are all myisam tables with the same structure, you may consider creating a merge table on them:
The MERGE storage engine, also known as the MRG_MyISAM engine, is a
collection of identical MyISAM tables that can be used as one.
“Identical” means that all tables have identical column and index
information.
I have such a situation, in my system there are many databases and for some queries I need to use the same table "hours", which basically has one field 'h' and stores hours like '00',...,'23'.
My question is about efficiency, is it better to create separate database and store this table there or have this table in each database. My queries will look like:
SELECT ... FROM hours CROSS JOIN some_table ...
Thank you!
Either way, you'd be having to modify all the "non-local" queries to use that table
SELECT ...
FROM sometable
CROSS JOIN dbname.hours
If it's stuck into its own database, then you have to modify ALL queries. If it's in one of the 'real' databases, you only have to modify n-1 queries. Plus having to grant appropriate permissions on that table if you're using multiple different mysql accounts for the different databases.
I have a table containing about 500 000 rows. Once a day, I will try to synchronize this table with an external API. Most of the times, there are few- or no changes made since last update. My question is basically how should I construct my MySQL query for best performance? I have thought about using insert ignore, but it doesn't feel like the best way to go since only a few rows will be inserted and MySQL must loop through all rows in the table. I have also thought about using LOAD_DATA_INFILE to insert all rows in a temporary table and then select the rows not already in my original table, and then remove the temporary table. Maybe someone else has a better suggestion?
Thank you in advance!
I usually use a temporary table and the LOAD DATA INFILE bulk loader. The bulk loader is much more efficient that trying to insert records using a dynamically created query.
If you index your permanent tables with appropriate unique keys that relate to the keys in the API then you should find the the INSERT and UPDATE statements work pretty fast. An example of the type of INSERT query I use is as follows:
INSERT INTO keywords(api_adgroup_id, api_keyword_id, keyword_text, match_type, status)
SELECT a.api_id, a.keyword_text, a.match_type, a.status
FROM tmp_keywords a LEFT JOIN keywords b ON a.api_adgroup_id = b.api_adgroup_id AND a.api_keyword_id = b.api_keyword_id
WHERE b.api_keyword_id IS NULL
In this example, I perform an OUTER JOIN on the keywords table to check if it already exists. Only new rows in the temporary table where there isn't a match in the main table (the api_keyword_id in the keywords table is NULL) are inserted.
Also note that in this example I need to use both the ad group id AND the keyword id to uniquely identify the keyword because the AdWords API gives the same keyword/match type combination the same id when it exists in more than one ad group.