搜索
首页数据库mysql教程特殊SQL语句及优化原则

1.按姓氏笔画排序: Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as 2.数据库加密: select encrypt('原始密码') select pwdencrypt('原始密码') select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同 encrypt

   1.按姓氏笔画排序:

  Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as

  2.数据库加密:

  select encrypt('原始密码')

  select pwdencrypt('原始密码')

  select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同 encrypt('原始密码')

  select pwdencrypt('原始密码')

  select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同

  3.取回表中字段:

  declare @list varchar(1000),@sql nvarchar(1000)

  select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='表A'

  set @sql='select '+right(@list,len(@list)-1)+' from 表A'

  exec (@sql)

  4.查看硬盘分区:

  EXEC master..xp_fixeddrives

  5.比较A,B表是否相等:

  if (select checksum_agg(binary_checksum(*)) from A)

  =

  (select checksum_agg(binary_checksum(*)) from B)

  print '相等'

  else

  print '不相等'

  6.杀掉所有的事件探察器进程:

  DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROMmaster.dbo.sysprocesses

  WHERE program_name IN('SQL profiler',N'SQL 事件探查器')

  EXEC sp_msforeach_worker '?'

  7.记录搜索:

  开头到N条记录

  Select Top N * From 表

  -------------------------------

  N到M条记录(要有主索引ID)

  Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID Desc

  ----------------------------------

  N到结尾记录

  Select Top N * From 表 Order by ID Desc

  8.如何修改数据库的名称:

  sp_renamedb 'old_name', 'new_name'

  9:获取当前数据库中的所有用户表

  select Name from sysobjects where xtype='u' and status>=0

  10:获取某一个表的所有字段

  select name from syscolumns where id=object_id('表名')

  11:查看与某一个表相关的视图、存储过程、函数

  select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'

  12:查看当前数据库中所有存储过程

  select name as 存储过程名称 from sysobjects where xtype='P'

  13:查询用户创建的所有数据库

  select * from master..sysdatabases D where sid not in(select sid from master..syslogins where)

  或者

  select dbid, name AS DB_NAME from master..sysdatabases where sid 0x01

  14:查询某一个表的字段和数据类型

  select column_name,data_type from information_schema.columns

  where table_name = '表名'

  [n].[标题]:

  Select * From TableName Order By CustomerName

  [n].[标题]:

  Select * From TableName Order By CustomerName

  --------------------------------------------------------------------------------------

  Sql优化是一项复杂的工作,以下的一些基本原则是本人看书时所记录下来的,很明确且没什么废话:

  1. 索引的使用:

  (1).当插入的数据为数据表中的记录数量的10%以上,首先需要删除该表的索引来提高数据的插入效率,当数据插入后,再建立索引。

  (2).避免在索引列上使用函数或计算,在where子句中,如果索引是函数的一部分,优化器将不再使用索引而使用全表扫描。如:

  低效:select * from dept where sal*12 >2500;

  高效:select * from dept where sal>2500/12;

  (3).避免在索引列上使用not和 “!=”,索引只能告诉什么存在于表中,而不能告诉什么不存在于表中,当数据库遇到not 和 “!=”时,就会停止使用索引而去执行全表扫描。

  (4).索引列上>=代替>

  低效:select * from emp where deptno > 3

  高效:select * from emp where deptno >=4

  两者的区别在于,前者dbms将直接跳到第一个deptno等于4的记录,而后者将首先定位到deptno等于3的记录并且向前扫描到第一个deptno大于3的。

  (5).非要对一个使用函数的列启用索引,,基于函数的索引是一个较好的方案。

  2. 游标的使用:

  当在海量的数据表中进行数据的删除、更新、插入操作时,用游标处理的效率是最慢的,但是游标又是必不可少的,所以正确使用游标十分重要:

  (1). 在数据抽取的源表中使用时间戳,这样每天的维表数据维护只针对更新日期为最新时间的数据来进行,大大减少需要维护的数据记录数。

  (2). 在insert和update维表时都加上一个条件来过滤维表中已经存在的记录,例如:

  insert into dim_customer select * from ods_customer where ods_customer.code not exists (dim_customer.code)

  ods_customer为数据源表。dim_customer为维表。

  (3). 使用显式的游标,因为隐式的游标将会执行两次操作,第一次检索记录,第二次检查too many rows这个exception,而显式游标不执行第二次操作。

  3. 据抽取和上载时的sql优化:

  (1). Where 子句中的连接顺序:

  oracle采用自下而上的顺序解析where子句,根据这个原理,表之间的连接必须写在其他where条件之前,那些可以过滤掉大量记录的条件必须写在where子句的末尾。如:

  低效:select * from emp e where sal>5000 and job = ‘manager’ and 25

  高效:select * from emp e where 255000 and job=’manager’;

  (2). 删除全表时,用truncate 替代 delete,同时注意truncate只能在删除全表时适用,因为truncate是ddl而不是dml。

  (3). 尽量多使用commit

  只要有可能就在程序中对每个delete,insert,update操作尽量多使用commit,这样系统性能会因为commit所释放的资源而大大提高。

  (4). 用exists替代in ,可以提高查询的效率。

  (5). 用not exists 替代 not in

  (6). 优化group by

  提高group by语句的效率,可以将不需要的记录在group by之前过滤掉。如:

  低效:select job, avg(sal) from emp group by job having job = ‘president’ or job=’manager’;

  高效: select job, avg(sal) from emp having job=’president’ or job=’manager’ group by job;

  (7). 有条件的使用union-all 替代 union:这样做排序就不必要了,效率会提高3到5倍。

  (8). 分离表和索引

  总是将你的表和索引建立在不同的表空间内,决不要将不属于oracle内部系统的对象存放到system表空间内。同时确保数据表空间和索引表空间置于不同的硬盘控制卡控制的硬盘上。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。