PHP Mysql 跨数据库连接 [英] PHP Mysql joins across databases

查看:40
本文介绍了PHP Mysql 跨数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在同一台物理服务器上有两个独立的数据库 X 和 Y.

Lets say I have two separate databases, X and Y, on the same physical server.

我所有的查询当前都用完了 X.

All of my queries currently run out of X.

我发现 Y 中有一张表,我希望 X 可以使用它进行 JOINS.

I find I have one table in Y I would like to be available to X for JOINS.

所以...现在我在 X 和 Y 中保留了 X 和 Y 所需的一个表的副本,但是 Y 中的数据不断变化,因此副本很快就会过时.对这个应用程序并不重要,但对于我现在正在设计的另一个应用程序来说,这是行不通的.

So... now I keep a copy of the one table I need for both X and Y in both X and Y, but the data in Y is constantly changing, so the copy soon becomes out of date. Not critical for this application, but for another I'm designing now this just won't do.

X 现在只需要对 Y 中的表进行读取访问.

X right now only needs read access to the table in Y.

是否有一种有效的方法可以跨两个数据库进行连接?

Is there an efficient way to do joins across the two databases?

我更喜欢 mysql 解决方案,而不是在 php 中打开多个数据库连接.

I'd prefer a mysql solution as opposed to opening multiple database connections in php.

谢谢.

那么这些行会变成什么?:

So what becomes of these lines?:

$conn = mysql_connect('localhost','username','password');
@mysql_select_db('database_name',$conn)

是的..这是一个旧的应用程序,因此是mysql而不是mysqli.假设我可以转换它.

And yes.. this is an old app, hence the mysql instead of mysqli. Suppose I could convert it.

所以我不指定 database_name.table_name 而只是指定 table_name 我得到任何选择的东西,我在那里指定 database.table_name 我得到了我想要的......对吗?是的.工作.

So where I don't specify database_name.table_name and just specify table_name I get whatever is selected, and where I do specify database.table_name I get what I asked for.. right? Yep. Works.

与在同一数据库内进行跨数据库连接相比,是否有任何显着的性能影响?不.看起来一样快.

Is there any significant performance hit by doing cross database joins versus joins within the same database? Nope. Looks just as fast.

感谢大家的回复.

推荐答案

那么这些行会变成什么?:

So what becomes of these lines?:

$conn =mysql_connect('本地主机','用户名','密码');@mysql_select_db('database_name',$conn)

$conn = mysql_connect('localhost','username','password'); @mysql_select_db('database_name',$conn)

mysql_select_db 是可选的.因为它只是选择要使用的活动数据库.http://us3.php.net/manual/en/function.mysql-select-db.php

The mysql_select_db is optional. As it just selects the active db to use. http://us3.php.net/manual/en/function.mysql-select-db.php

是的..这是一个旧的应用程序,因此mysql而不是mysqli.假设我可以转换它.

And yes.. this is an old app, hence the mysql instead of mysqli. Suppose I could convert it.

恕我直言,我认为使用 MYSQL 与速度慢没有任何关系,从我自己的研究来看,MYSQLi 与 MYSQL 的速度提升不大.现实世界的测试并没有表明它是一个很大的性能驱动因素.老实说,我的所有应用程序都坚持使用 MYSQL.

IMHO I don't think using MYSQL has anything to do with being slow, from my own research into it, there is not much speed improvement with MYSQLi vs MYSQL. Real world tests don't show that being a big performance driver. I've honestly stuck with MYSQL for all my apps.

这篇关于PHP Mysql 跨数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆