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

查看:225
本文介绍了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?

相对于在php中打开多个数据库连接,我更喜欢mysql解决方案.

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('localhost','用户名','密码'); @mysql_select_db('数据库名称',$ conn)

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

mysql_select_db是可选的.因为它只是选择要使用的活动数据库. http://us3.php.net/manual/zh/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天全站免登陆