关于包含函数表达式的复合索引优化查询
圈内好友有一个sql语句需要优化,sql语句和执行计划如下: SELECT 2 MAX(tt.workitem_id) workitem_id, 3 tt.task_id 4 FROM 5 doudou tt 6 WHERE 7 tt.position_id =5 8 AND TO_CHAR(tt.pos_rcv_datim, 'yyyymmdd') =20140815 9 GROUP BY 10 tt.task_id; 67
圈内好友有一个sql语句需要优化,sql语句和执行计划如下:<br>
SELECT<br>
2 MAX(tt.workitem_id) workitem_id,<br>
3 tt.task_id<br>
4 FROM<br>
5 doudou tt<br>
6 WHERE<br>
7 tt.position_id =5<br>
8 AND TO_CHAR(tt.pos_rcv_datim, 'yyyymmdd') =20140815<br>
9 GROUP BY<br>
10 tt.task_id;
670 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3539805324
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1797 | 88053 | 4570 (1)| 00:00:55 |
| 1 | HASH GROUP BY | | 1797 | 88053 | 4570 (1)| 00:00:55 |
|* 2 | TABLE ACCESS FULL| DOUDOU | 1800 | 88200 | 4569 (1)| 00:00:55 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("TT"."POSITION_ID"=5 AND
TO_NUMBER(TO_CHAR(INTERNAL_FUNCTION("TT"."POS_RCV_DATIM"),'yyyymmdd'))=2
0140815)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
16633 consistent gets
16630 physical reads
0 redo size
35014 bytes sent via SQL*Net to client
1007 bytes received via SQL*Net from client
46 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
670 rows processed
看出这里走的全表扫描,可能一般朋友能否去掉TO_CHAR,建立(pos_rcv_datim,position_id)的复合索引,但是这个sql是不允许修改的,那么复合索引没办法了吗,其实不然我们是可以建立包含函数表达式的复合索引的
create index ind_doudou04 on doudou(TO_CHAR(tt.pos_rcv_datim, 'yyyymmdd'),position_id)然后再看最新的执行计划:
Execution Plan
----------------------------------------------------------
Plan hash value: 1864030226
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1862 | 87514 | 50 (2)| 00:00:01|
| 1 | HASH GROUP BY | | 1862 | 87514 | 50 (2)| 00:00:01|
|* 2 | INDEX RANGE SCAN| IND_DOUDOU4 | 1864 | 87608 | 49 (0)| 00:00:01|
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access(TO_CHAR(INTERNAL_FUNCTION("POS_RCV_DATIM"),'yyyymmdd')='201
40815' AND "TT"."POSITION_ID"=5)
filter("TT"."POSITION_ID"=5)
Statistics
----------------------------------------------------------
17 recursive calls
0 db block gets
62 consistent gets
52 physical reads
0 redo size
35014 bytes sent via SQL*Net to client
1007 bytes received via SQL*Net from client
46 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
670 rows processed
上面这个sql的优化并不难,而这里小鱼想展示的是,可能平常我们所看见的复合索引多半是(col1,col2,…coln) 这类,很少有创建包含(col1,func(col,func_name))这类复合索引,有些东西不要想当然以为可能或者不可能,oracle更多是个实际的动手的东西。
原文地址:关于包含函数表达式的复合索引优化查询, 感谢原作者分享。

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

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

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
