当前位置:网站首页>Three schemes of SQL query across the table
Three schemes of SQL query across the table
2022-08-10 18:45:00 【dry road】
前言
A friend of mine asked me recently,如何进行sqlcross-database association query? 首先呢,我们知道mysql是不支持跨库连接的,但是老话说得好,只要思想不滑坡,Ideas are always more difficult than difficult!
PS:The problem lies here,Still can not solve what is?
After some thought I came up with three options for him,Although not perfect,But each leads the pack!
My connection plan,以postgreSql库为例.
方案一:Link multiple libraries,Execute the query synchronously
The specific idea is to connect multiple libraries separately in the code,After finding the required data in a library,通过关键字段,Synchronous execution goes to other libraries to query related data,Then perform the required data analysis or update!
优点
- 可以进行实时查询;
- The data can be modified on demand and the return value can be modified within the logical range;
- This scheme is generally used,Query data will be paginated query,Or the query conditions are precise,So the amount will be smaller,对服务器压力小;
- Server static analysis data,效率高;
缺点
- Not suitable for bulk data writing/查询,It will cause the database connection to time out or the obtained data flow to be too large, resulting in a large amount of server memory usage;
- 同步执行策略,Querying the database takes time proportional to the runtime;
代码执行
Some simple code logic,No one will understand it~~~
postgreSql.js
//链接多个数据库,并暴露
const pg = require('pg');
const sqlConfig = {
testOnePgSql: {
user: "postgres",
database: "admindb",
password: "123",
host: "192.168.1.111",//数据库ip地址(Randomly written,Write your own libraryip哈)
port: 5432, // 扩展属性
max: 20, // 连接池最大连接数
idleTimeoutMillis: 3000
},
//Super island merchants
testTwoPgSql: {
//测试数据库
user: "postgres",
database: "admindb",
password: "123",
host: "192.168.1.112",//数据库ip地址(Randomly written,Write your own libraryip哈)
port: 5432, // 扩展属性
max: 20, // 连接池最大连接数
idleTimeoutMillis: 3000
},
//Myrtle Merchant
testThreePgSql: {
//测试数据库
user: "postgres",
database: "admindb",
password: "123",
host: "192.168.1.113",//数据库ip地址(Randomly written,Write your own libraryip哈)
port: 5432, // 扩展属性
max: 20, // 连接池最大连接数
idleTimeoutMillis: 3000
},
};
const testOnePgSql = new pg.Pool(sqlConfig.banuPgSql);
const testTwoPgSql = new pg.Pool(sqlConfig.testTwoPgSql);
const testThreePgSql = new pg.Pool(sqlConfig.testThreePgSql);
module.exports = {
testOnePgSql,
testTwoPgSql,
testThreePgSql
};
复制代码
封装查询pgsql方法
postgreSqlClass.js
let sqlMap = require('./postgreSql');
module.exports = {
/** *查询pgsql数据 * @param sqlSelect 查询语句 string * @param tenancy 商户id string */
select(sqlSelect, tenancy) {
//按需连接
let pool = sqlMap[tenancy];
return new Promise((resolve, reject) => {
pool.connect(async function (err, connection) {
if (err) {
// 结束会话
connection.release();
return reject(err);
}
let result = await pgQuery(sqlSelect, connection);
// 结束会话
connection.release();
return resolve(result);
});
});
}
};
/** * pgsql查询数据 * @param sqlQuery 查询语句 * @param connection pgSql连接后的connection * @returns {Promise<unknown>} */
async function pgQuery(sqlQuery, connection) {
return new Promise((resolve, reject) => {
connection.query(sqlQuery, (err, rows) => {
if (err) return reject(err);
return resolve(rows.rows || []);
});
});
}
复制代码
Now proceed to the business module
test.js
"use strict";
//引入pg函数
let PGSQL = require("./postgreSqlClass");
exports.getUserList = async () => {
let sqlOneSelect = `${The first table query statement}`;
let userList = await PGSQL.select(sqlSelect, "testOnePgSql");
//获取对应two表的数据
//...逻辑
let sqlTwoSelect = `${The first table query statement}`;
let userListTwo = await PGSQL.select(sqlTwoSelect, "testTwoPgSql");
let result = [];
//Combine the data you want
//...逻辑
return result;
};
复制代码
方案二:Add redundant tables in the main database,通过定时更新,Causes the query of the same database linked table
比如AThe library is the main database,B、CFor other add-on libraries,We need to combine the three librariesuserThe table performs data join table query; 具体思路为:
- 在A库存在user表,Redundant tables are created at this pointuser_two、user_three表,and corresponding fieldsB、C库的user表字段;
- 通过代码逻辑,进行定时任务,将B、C表,数据更新至A库user_two、user_three表;
- Data analysis is required/查询时,仅查询A库即可,但需要将A库的user、user_two、user_threeTables are available on demand;
优点
- Convert cross-table queries to same-table queries,The execution logic is simpler;
- It can perform big data analysis and big data query;
- Data can be preprocessed,Improve analysis speed;
缺点
- 定时更新,Not timely;
- The corresponding table needs to have the last update time field,Otherwise, there will be more synchronization data;
- 增加冗余表,It will cause the main table space occupancy rate to increase;
- 定时更新,It will cause a large amount of data to be written at a certain point in time/修改数据,Data reading may be affected,因此,Multi-node deployment is recommended(读写、只读);
Similar implementation scenarios
- T+1Time report display;
- The local area network database information is reported to the online database;
方案三:(极度不建议)dbLinkLink multiple libraries natively,Perform data analysis locally
具体思路:
- dblink就是我们在创建表的时候连接到我们的远程库,Then the data of the newly created table locally is the data of the mapped remote table.
- 当我们创建一个以FEDERATED为存储引擎的表时,服务器在数据库目录只创建一个表定义文件.文件由表的名字开始,并有一个frm扩展名.无其它文件被创建,因为实际的数据在一个远程数据库上.这不同于为本地表工作的存储引擎的方式.
执行步骤:
- 1.如我现在本地要连接我的阿里云的sys_user表,So I need to build a table with the same fields locally,我取名叫sys_user_copy,并连接到远程库,建好后,我本地sys_user_copy的表里面的数据是映射远程的表的数据
- 2.所以我关联查询,可以直接关联我本地sys_user_copy表从而查出来.改了本地的数据,远程的表数据也会跟着变
- **开启FEDERATED引擎,**show engines
如果这里是NO,需要在配置文件[mysqld]中加入一行:federated
改完重启服务,就变成yes了.
- 4 建表时加上连接
CREATE TABLE (......)
ENGINE =FEDERATED CONNECTION='mysql://username:[email protected]:port/database/tablename'
复制代码
优点
- 不需要程序员介入,不需要开发
- Quick results,If you just want to query some data
缺点
- 本地表结构必须与远程表完全一样
- 不支持事务
- 不支持表结构修改
- 删除本地表,远程表不会删除
- 远程服务器必须是一个MySQL服务器
- Database data is not written locally,Essentially a soft link,Querying a large amount of data can cause the local memory to fill up,Because it is querying data from multiple databases to local memory,Then do the calculations in memory,此时时间复杂度为O(N^2),空间复杂度也为O(N^2);500条数据,The corresponding local time complexity is 25W,时间复杂度为25W;
可用于:Data import between two libraries,不涉及计算,即A导入B,不进行查询A\BDo a computational writeC;
The idea of Scheme 3 is drawn from the following article blog.csdn.net/qq_48721706…
结语
好的算法,It is also based on a good execution plan,所以,Three programs can be used as needed!
最后,If you have any other good solutions,Welcome to discuss with me~~~
边栏推荐
- 【2015】【论文笔记】等离子光混合器THz辐射的光谱——
- 智能出价策略如何影响广告效果?
- 20220810
- 【图像分割】基于元胞自动机实现图像分割附matlab代码
- 「业务架构」业务能力的热图是什么,有啥用?
- 1720. 解码异或后的数组
- 2022-08-09 Study Notes day32-IO Stream
- requires ‘angle‘ attribute to be a multiple of 45
- 【FAQ】【Push Kit】推送服务,回执配置一直报错、回执过期修改、怎么删除配置的回执
- redis.exceptions.DataError: Invalid input of type: ‘dict‘. Convert to a byte, string or number first
猜你喜欢
随机推荐
Thoughts on Technology Sharing
微服务架构-实现技术之六大基础组件:服务通信+事件驱动+负载均衡+服务路由+API网关+配置管理
Redis命令---key篇 (超全)
测试接口出现“data“: “Full authentication is required to access this resource“凭证已过期
requires ‘angle‘ attribute to be a multiple of 45
组合模式
开发模式对测试的影响
CSV(Comma-Separate-Values)逗号分隔值文件
20220810
Unity_Stack<T>()的应用(多个次级界面后的返回逻辑)
NPDP|传统行业产品经理如何进行能力提升?
搭载2.8K 120Hz OLED华硕好屏 无畏Pro15 2022锐龙版屏开得胜
Allegro软件Shape菜单下的每个命令的含义
面试题 04.12. 求和路径-dfs+辅助数组法
剑指 Offer II 034. 外星语言是否排序-辅助数组法
Keil5退出仿真调试卡死的解决办法
三坐标雷达显示软件 SPx Viewer-3D
多线程与高并发(五)—— 源码解析 ReentrantLock
Toronto Research Chemicals农药检测丨甲硫威
【FAQ】OpenHarmony与HarmonyOS的有什么区别?