mysql多表关联查询,增加了排序速度慢??
mysql
这些是表的结构:
CREATE TABLE rs_emp
(EmpSysID
varchar(36) NOT NULL,EmpNo
varchar(20) NOT NULL,EmpName
varchar(50) NOT NULL DEFAULT '',EmpSexSysID
varchar(36) DEFAULT NULL,CardTypeSysID
varchar(36) DEFAULT NULL,RateSysID
varchar(36) DEFAULT NULL,JobSysID
varchar(36) DEFAULT NULL,DepartSysID
varchar(36) NOT NULL,StatusSysID
varchar(36) DEFAULT NULL,EmpEngName
varchar(50) DEFAULT NULL,EmpHireDate
datetime DEFAULT NULL,EmpPositiveDate
datetime DEFAULT NULL,EmpLeaveDate
datetime DEFAULT NULL,EmpLeaveReason
varchar(255) DEFAULT NULL,EmpStatusID
tinyint(4) DEFAULT NULL,EmpMemo
varchar(200) DEFAULT NULL,CertTypeSysID
varchar(36) DEFAULT NULL,EmpCertNo
varchar(50) DEFAULT NULL,EmpBirthDate
varchar(50) DEFAULT NULL,CountrySysID
varchar(36) DEFAULT NULL,NationSysID
varchar(36) DEFAULT NULL,EduSysID
varchar(36) DEFAULT NULL,EmpGdSchool
varchar(200) DEFAULT NULL,PolitySysID
varchar(36) DEFAULT NULL,EmpLanguageAbility
varchar(200) DEFAULT NULL,EmpMarrySysID
varchar(36) DEFAULT NULL,EmpZipNo
varchar(50) DEFAULT NULL,EmpPhoneNo
varchar(50) DEFAULT NULL,NativeSysID
varchar(36) DEFAULT NULL,EmpAddress
varchar(200) DEFAULT NULL,EmpEmail
varchar(200) DEFAULT NULL,EmpFamilyInfo
varchar(200) DEFAULT NULL,EmpBiography
mediumtext,AllowDelete
bit(1) DEFAULT b'1',EmpLeaveStatus
bit(1) DEFAULT b'0',IsAttend
char(1) DEFAULT NULL,OtherCardNo
varchar(10) DEFAULT NULL,CardFingerNo
bigint(10) DEFAULT NULL,
PRIMARY KEY (EmpSysID
),
UNIQUE KEY AK_RS_Emp
(EmpNo
),
KEY AK_RS_Emp2
(EmpSysID
),
KEY AK_RS_Emp3
(EmpName
),
KEY AK_RS_Emp6
(RateSysID
),
KEY AK_RS_Emp7
(StatusSysID
),
KEY AK_RS_Emp8
(CountrySysID
),
KEY AK_RS_Emp9
(CertTypeSysID
),
KEY AK_RS_Emp10
(EduSysID
),
KEY AK_RS_Emp11
(PolitySysID
),
KEY AK_RS_Emp12
(NationSysID
),
KEY AK_RS_Emp23
(EmpSysID
,EmpNo
),
KEY AK_RS_Emp33
(EmpLeaveStatus
),
KEY AK_RS_Emp22
(EmpSysID
,EmpName
)
) ENGINE=MyISAM DEFAULT CHARSET=gbk
CREATE TABLE rs_depart
(DepartSysID
varchar(36) NOT NULL,DepartPrcID
varchar(100) NOT NULL,DepartID
varchar(12) NOT NULL,DepartName
varchar(50) NOT NULL,DepartUpSysId
varchar(36) DEFAULT NULL,DepartMemo
varchar(200) DEFAULT NULL,
PRIMARY KEY (DepartSysID
),
UNIQUE KEY AK_RS_Depart
(DepartID
) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=gbk
CREATE TABLE rs_classcerttype
(CertTypeSysID
varchar(36) NOT NULL,CertTypeNo
varchar(20) NOT NULL,CertTypeName
varchar(50) NOT NULL,
PRIMARY KEY (CertTypeSysID
),
UNIQUE KEY AK_RS_ClassCertType
(CertTypeNo
)
) ENGINE=MyISAM DEFAULT CHARSET=gbk
CREATE TABLE rs_classcountry
(CountrySysID
varchar(36) NOT NULL,CountryNo
varchar(20) NOT NULL,CountryName
varchar(50) NOT NULL,
PRIMARY KEY (CountrySysID
),
UNIQUE KEY AK_RS_ClassCountry
(CountryNo
)
) ENGINE=MyISAM DEFAULT CHARSET=gbk
跟rs_classcountry类似结构的表还有几张
最后创建视图,为这几张表的查询结果,视图:
CREATE ALGORITHM=UNDEFINED DEFINER=root
@%
SQL SECURITY DEFINER VIEW vrs_emp
AS select a
.EmpSysID
AS EmpSysID
,a
.EmpNo
AS EmpNo
,a
.EmpName
AS EmpName
,a
.CardFingerNo
AS CardFingerNo
,a
.EmpSexSysID
AS EmpSexSysID
,a
.CardTypeSysID
AS CardTypeSysID
,a
.RateSysID
AS RateSysID
,a
.JobSysID
AS JobSysID
,a
.DepartSysID
AS DepartSysID
,a
.StatusSysID
AS StatusSysID
,a
.EmpEngName
AS EmpEngName
,a
.EmpHireDate
AS EmpHireDate
,a
.EmpPositiveDate
AS EmpPositiveDate
,a
.EmpLeaveDate
AS EmpLeaveDate
,a
.EmpLeaveReason
AS EmpLeaveReason
,a
.EmpStatusID
AS EmpStatusID
,a
.EmpMemo
AS EmpMemo
,a
.CertTypeSysID
AS CertTypeSysID
,a
.EmpCertNo
AS EmpCertNo
,a
.EmpBirthDate
AS EmpBirthDate
,a
.CountrySysID
AS CountrySysID
,a
.NationSysID
AS NationSysID
,a
.EduSysID
AS EduSysID
,a
.EmpGdSchool
AS EmpGdSchool
,a
.PolitySysID
AS PolitySysID
,a
.EmpLanguageAbility
AS EmpLanguageAbility
,a
.EmpMarrySysID
AS EmpMarrySysID
,a
.EmpZipNo
AS EmpZipNo
,a
.EmpPhoneNo
AS EmpPhoneNo
,a
.NativeSysID
AS NativeSysID
,a
.EmpAddress
AS EmpAddress
,a
.EmpEmail
AS EmpEmail
,a
.EmpFamilyInfo
AS EmpFamilyInfo
,a
.EmpBiography
AS EmpBiography
,a
.AllowDelete
AS AllowDelete
,a
.EmpLeaveStatus
AS EmpLeaveStatus
,a
.IsAttend
AS IsAttend
,a
.OtherCardNo
AS OtherCardNo
,(to_days(now()) - to_days(a
.EmpPositiveDate
)) AS EmpPositiveDay
,floor(((to_days(curdate()) - to_days(a
.EmpBirthDate
)) / 365)) AS EmpAge
,b
.DepartID
AS DepartID
,b
.DepartName
AS DepartName
,c
.StatusNo
AS StatusNo
,c
.StatusName
AS StatusName
,d
.RateNo
AS RateNo
,d
.RateName
AS RateName
,e
.PolityNo
AS PolityNo
,e
.PolityName
AS PolityName
,f
.NativeNo
AS NativeNo
,f
.NativeName
AS NativeName
,g
.NationNo
AS NationNo
,g
.NationName
AS NationName
,h
.JobNo
AS JobNo
,h
.JobName
AS JobName
,i
.EduNo
AS EduNo
,i
.EduName
AS EduName
,j
.CountryNo
AS CountryNo
,j
.CountryName
AS CountryName
,k
.CertTypeNo
AS CertTypeNo
,k
.CertTypeName
AS CertTypeName
,m
.EmpSexID
AS EmpSexID
,m
.EmpSexName
AS EmpSexName
,n
.EmpStatusName
AS EmpStatusName
,o
.EmpMarryID
AS EmpMarryID
,o
.EmpMarryName
AS EmpMarryName
,p
.CardPhysicsNo10
AS CardPhysicsNo10
,p
.CardPhysicsNo8
AS CardPhysicsNo8
,p
.CardSectorNo
AS CardSectorNo
,p
.CardStatusID
AS CardStatusID
,p
.CardStatusDate
AS CardStatusDate
,p
.CardPWD
AS CardPWD
,p
.CardStartDate
AS CardStartDate
,p
.CardEndDate
AS CardEndDate
,p
.CardUseDate
AS CardUseDate
,p
.CardUseTimes
AS CardUseTimes
,p
.RetirementFlag
AS RetirementFlag
,p
.CardBTMoney
AS CardBTMoney
,p
.FaDate
AS FaDate
,q
.CardStatusName
AS CardStatusName
,r
.EmpPhotoPath
AS EmpPhotoPath
from ((((((((((((((((rs_emp
a
join rs_depart
b
on((b
.DepartSysID
= a
.DepartSysID
))) left join rs_classstatus
c
on((c
.StatusSysID
= a
.StatusSysID
))) left join rs_classrate
d
on((d
.RateSysID
= a
.RateSysID
))) left join rs_classpolity
e
on((e
.PolitySysID
= a
.PolitySysID
))) left join rs_classnative
f
on((f
.NativeSysID
= a
.NativeSysID
))) left join rs_classnation
g
on((g
.NationSysID
= a
.NationSysID
))) left join rs_classjob
h
on((h
.JobSysID
= a
.JobSysID
))) left join rs_classeducation
i
on((i
.EduSysID
= a
.EduSysID
))) left join rs_classcountry
j
on((j
.CountrySysID
= a
.CountrySysID
))) left join rs_classcerttype
k
on((k
.CertTypeSysID
= a
.CertTypeSysID
))) left join vrs_empsex
m
on((m
.EmpSexSysID
= a
.EmpSexSysID
))) left join vrs_empstatus
n
on((n
.EmpStatusID
= a
.EmpStatusID
))) left join vrs_empmarrystatus
o
on((o
.EmpMarrySysID
= a
.EmpMarrySysID
))) left join rs_empcard
p
on((p
.EmpSysID
= a
.EmpSysID
))) left join vrs_empcardstatus
q
on((p
.CardStatusID
= q
.CardStatusID
))) left join rs_empphoto
r
on((a
.EmpSysID
= r
.EmpSysID
))) where (a
.EmpLeaveStatus
1)
当我在mysql 用explain执行了一下 explain select * from vrs_emp where empsysid'' order by empno limit 0,30
出现
1 SIMPLE b system PRIMARY 1Using temporary; Using filesort
该如何优化??都有索引

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

大数据结构处理技巧:分块:分解数据集并分块处理,减少内存消耗。生成器:逐个产生数据项,无需加载整个数据集,适用于无限数据集。流:逐行读取文件或查询结果,适用于大文件或远程数据。外部存储:对于超大数据集,将数据存储在数据库或NoSQL中。

在PHP中备份和还原MySQL数据库可通过以下步骤实现:备份数据库:使用mysqldump命令转储数据库为SQL文件。还原数据库:使用mysql命令从SQL文件还原数据库。

可以通过以下方式优化MySQL查询性能:建立索引,将查找时间从线性复杂度降至对数复杂度。使用PreparedStatements,防止SQL注入并提高查询性能。限制查询结果,减少服务器处理的数据量。优化连接查询,包括使用适当的连接类型、创建索引和考虑使用子查询。分析查询,识别瓶颈;使用缓存,减少数据库负载;优化PHP代码,尽量减少开销。

如何将数据插入MySQL表中?连接到数据库:使用mysqli建立与数据库的连接。准备SQL查询:编写一个INSERT语句以指定要插入的列和值。执行查询:使用query()方法执行插入查询,如果成功,将输出一条确认消息。

使用PHP创建MySQL表需要以下步骤:连接到数据库。创建数据库(如果不存在)。选择数据库。创建表。执行查询。关闭连接。

要在PHP中使用MySQL存储过程:使用PDO或MySQLi扩展连接到MySQL数据库。准备调用存储过程的语句。执行存储过程。处理结果集(如果存储过程返回结果)。关闭数据库连接。

MySQL 8.4(截至 2024 年的最新 LTS 版本)中引入的主要变化之一是默认情况下不再启用“MySQL 本机密码”插件。此外,MySQL 9.0完全删除了这个插件。 此更改会影响 PHP 和其他应用程序

Oracle数据库和MySQL都是基于关系模型的数据库,但Oracle在兼容性、可扩展性、数据类型和安全性方面更胜一筹;而MySQL则侧重速度和灵活性,更适合小到中等规模的数据集。①Oracle提供广泛的数据类型,②提供高级安全功能,③适合企业级应用程序;①MySQL支持NoSQL数据类型,②安全性措施较少,③适合小型到中等规模应用程序。
