I guess we can do database1.table1 join database2.table2 on etc. I.e. join tables in separate databases on the same server.
Is it possible to join tables across different sql servers?
you can use FEDERATED Storage Engine.
The FEDERATED storage engine lets you access data from a remote
MySQL database without using replication or cluster technology.
Querying a local FEDERATED table automatically pulls the data
from the remote (federated) tables. No data is stored on the local tables.
The site shows a good illustration
Related
I have some questions before implement the following scenario:
I have the Database A (it contains multiple tables with lots of data, and is being queried by multiple clients)
this database contains a users table, which I need to create some triggers, but this database is managed by a partner. We don't have permissions to create triggers.
And the Database B is managed by me, much lighter, the queries are only from one source, and I need to have access to users table data from Database A so I can create triggers and take actions for every update, create or delete in users table from database A.
My most concern is, how can this federated table impact on performance in database A? Database B is not the problem.
Both databases stay in the same geographic location, just different servers.
My goal is to make possible take actions from every transaction in database A users table.
Definitely queries that read federated tables have performance issues.
https://dev.mysql.com/doc/refman/8.0/en/federated-usagenotes.html says:
A FEDERATED table does not support indexes in the usual sense; because access to the table data is handled remotely, it is actually the remote table that makes use of indexes. This means that, for a query that cannot use any indexes and so requires a full table scan, the server fetches all rows from the remote table and filters them locally. This occurs regardless of any WHERE or LIMIT used with this SELECT statement; these clauses are applied locally to the returned rows.
Queries that fail to use indexes can thus cause poor performance and network overload. In addition, since returned rows must be stored in memory, such a query can also lead to the local server swapping, or even hanging.
(emphasis mine)
The reason the federated engine was created was to support applications that need to write to tables at a rate greater than a single server can support. If you are inserting to a table and overwhelming the I/O of that server, you can use a federated table so you can write to a table on a different server.
Reading from federated tables is likely to be worse than reading local tables, and cannot be optimized with indexes.
If you need good performance, you should use replication or a CDC tool, to maintain a real table on server B that you can query as a local table, not a federated table.
Another solution would be to cache the user's table in the client application, so you don't have to read it on every query.
We want to have schemas/database for each functionality on our MySQL Server; so that if load on DB grows; we can move that particular schema to independent server anyday.
How to restrict Joins across Schemas in MySQL ?
Is there any way to use the join operation for two tables from different databases?
Both databases are on different server.
If both databases are MySQL, then FEDERATED ENGINE provides functionality to join MySQL databases in different servers.
And read the limitations too.
Wikipedia:
The MySQL Federated storage engine for the MySQL relational database management system is a storage engine which allows a user to create a table that is a local representation of a foreign (remote) table. It utilizes the MySQL client library API as a data transport, treating the remote data source the same way other storage engines treat local data sources whether they be MYD files (MyISAM), memory (Cluster, Heap), or tablespace (InnoDB). Each Federated table that is defined there is one .frm (data definition file containing information such as the URL of the data source). The actual data can exist on a local or remote MySQL instance.
To create a Federated table, one has to specify a URL in the "CONNECTION" string:
create table t1 (
a int,
b varchar(32))
ENGINE=FEDERATED CONNECTION='mysql://user#hostname/test/t1'
The connection URL is in the format of:
scheme://user:pass#host:port/schema/tablename
I have 2 tables in different databases in different mysql hosts. can i write a single SQL statement to implement the inner join operation?
You can try to use federated table. Read this and this for limitation on using it.
Depending on your MySQL version, you might be be able to use Federated Storage Engine. Refer to Accessing Distributed Data with the Federated Storage Engine for more information.
That would mean connecting to another MySQL host from INSIDE the SQL statement. To my knowledge, this is not possible and I would regard it as highly dangerous if it were.
It is not possible to join two tables from different hosts. You can store the result from one of the tables in a hash keyed on the join attribute, and then perform the join operation in your program.
it is not possible to connect to two databases in same query
I have a relatively light query that needs information from a local MySQL table along with another MySQL table which is stored on a physically separate machine (on the same network). I'm keen to avoid setting up replication just to facilitate this light query that only needs executed once a day.
Is there any way that I can join with a table on a remote machine using one query? Or run a SELECT INTO into a local table.
Notes
I'm using C# & .NET 4.
This can be done by using the FEDERATED storage engine for the remote table. Find out more.