Cross table join using MYSQL rather than MSSQL - mysql

Is it possible to do a cross table join in mysql spaning different tables? in different databases.
This seem to be easily possible in MSSQL, and greatly speeds up data transfer?
How about mysql, do you need to use a powerful IDE to achieve this? or do you have to write a program to do something like this?
UPDATE tblUser
SET tblUser.ReceiveInfo=old_database.dbo.contact.third_party_info_yn
FROM tblUser inner join old_database.dbo.contact
ON old_database.dbo.contact.id=tblUser.oldid

Sure, very easy. Prefix the table name with the database name, like you show. We do cross-database joins on a regular basis. For example:
SELECT COUNT(*) FROM newusers1.users
JOIN newusers2.users
It certainly won't speed up data transfer compared to having both tables in the same database, though.
Now, if your tables are sitting on different database servers, the answer is no. An example of this is if your database is too big and you need to shard your tables. Things get more than a little messy. But given that you seem to be happy with the MS SQL solution, that does not seem to apply here.

In MySQL you can do cross DB joins and, by way of the FEDERATED engine, even cross server joins.

MySQL doesn't actually care if the tables are in the same "database", it's just a logical collection of tables for convenient administration, permissions etc.
So you can do a join between them just as easily as if they were in the same one (see ChrisInEdmonton's answer)

Related

MySQL: Is using a view faster than running 5+ JOIN statements?

I have a very long query in MySQL (MariaDB) which runs multiple JOIN and LEFT JOIN to 5 or more tables (InnoDB). Is it better to convert this to a view instead?
A view is just a stored SQL, nothing more. It is going to be executed in the exact same way. It can, however, be convenient to be able to query one single object instead of writing messy join everywhere.
Views are an organizational tool, not a performance enhancement tool.
Also See:
Performance of VIEW vs. SQL statement

create view on different databases on different hosts

Is it possible to create a view from tables from two different databases? Like:
creative view 'my_view' as
select names as value
from host_a.db_b.locations
union
select description as value
from host_b.db_b.items;
They currently are different database engines (MyISAM and InnoDB).
thx in advance
Yes, you need to access the remote table via the FEDERATED db engine, then create a view using your query.
However this is a rather messy way to solve the problem - particularly as (from your example query) your data is effectively sharded.
This structure won't allow updates/inserts on the view. Even for an updatable/insertable view, my gut feeling is that you'll run into problems if you try to anything other than auto-commit transactions, particularly as you're mixing table types. I'd recommend looking at replication as a better way to solve the problem.

join tables from multiple mysql host

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

querying for data from two tables in different databases

Initially I thought this would be a stupid question, but now I am inspired by the following question.
Background: I have a lot of data in MySQL, but MySQL's spatial support is terrible. Ideally I would like to migrate everything to Postgres, but converting from MySQL to Postgres is a massive ball of hurt (I've already wasted close to a week struggling with it). Now I am thinking, if only I could maintain only the spatial portion in Pg, do the spatial queries in Pg, then use those row ids to query non-spatial data from MySQL.
I am a Perl DBI person. My question is thus -- can I create a single database handle that actually allows querying by JOINing a table from Pg with a table from MySQL, assuming they have a common id column?
No, you will need to query both separately and combine the data at the application layer. See a more informed answer here:
How to create linked server MySQL
No, I don't think you could do it that way. You would have to query the data separately and combine the results in your code. I believe there are no REAL RDB's that can do what you want.

performance effect of joining tables from different databases

I have a web site using a database named lets say "site1". I am planning to put another site on the same server which will also use some of the tables from "site1".
So should I use three different databases like "site1" (for first site specific data), "site2" (for second site specific data), and "general" (for common tables). In which there will be join statements between databases general and site1 and site2. Or should I put all tables in one database?
Which is the best practice to do?
How performances differ in each situation?
I am using MySQL. So how is the situation especially for MySQL?
Thanks in advance...
From the performance point of view, there won't be ANY difference. Just keep your indexes in place and you will not notice whether you are using single DB or multiple DBs.
Apart from performance, there are 2 small implications that I can think of:
1. You can not have foreign keys across DBs.
2. Partitioning tables in DB based on their usage or based on applications can help you manage permissions in easy way.
I can speak from recent personal experience. I have some old mysql queries in some PHP code that worked fine with a relatively small database, but as it grew the query got slower and slower.
I have freeradius running mysql in its own database along with another management php app that I wrote. The freeradius table is > 1.5 million rows. I was attempting to join tables from my app's database to the freeradius database. I can say for sure 1.5 million rows is too many. Running some queries locked up my app altogether. I ended up having to re-write portions of my php app to do things differently (ie not joining 2 tables from different database). I also indexed the radius accounting table on some key fields and optimized some queries (mysql EXPLAIN statement is wonderful to help with this). Things are MUCH faster now.
I will definitely be hesitant to join 2 tables from different databases in the future unless really really necessary.