Java架构师成长直通车

78 阅读1分钟

XZ:Java架构师成长直通车

国内外一线大厂技术大咖与某课网组成专家团队12个月磨一剑
千万级电商项目从0到1到100全过程
涵盖Java程序员不同成长阶段的问题及优选解决方案

函數操作

對條件字段做函數操作走不了索引。

select * from t1 where date© =‘2019-05-21’;

優化:改成範圍查询

select * from t1 where c>=‘2019-05-21 00:00:00’ and c<=‘2019-05-21 23:59:59’;

隱式轉換

操作符與不同類型的操作對象一同運用時,就會發作類型轉換以使操作兼容。

select user_name,tele_phone from user_info where tele_phone =11111111111; /* tele_phone varchar */

實践會做函數操作:

select user_name,tele_phone from user_info where cast(tele_phone as singed int) =11111111111;

優化:類型統一

select user_name,tele_phone from user_info where tele_phone =‘11111111111’;

含糊查询

通配符在前面

select * from t1 where a like ‘%1111%’;

優化:含糊查询必需包含條件字段前面的值

select * from t1 where a like ‘1111%’;

範圍查询

範圍查询數據量太多,需求回表,因而不走索引。

select * from t1 where b>=1 and b <=2000;

優化:降低單次查询範圍,分屢次查询。(實践可能速度沒得快太多,倡議走索引)

select * from t1 where b>=1 and b <=1000;

show profiles;

±---------±-----------±-----------------------------------------+

| Query_ID | Duration | Query |

±---------±-----------±-----------------------------------------+

| 1 | 0.00534775 | select * from t1 where b>=1 and b <=1000 |

| 2 | 0.00605625 | select * from t1 where b>=1 and b <=2000 |

±---------±-----------±-----------------------------------------+

2 rows in set, 1 warning (0.00 sec)

計算操作

即便是简單的計算

explain select * from t1 where b-1 =1000;

優化:將計算操作放在等號後面

explain select * from t1 where b =1000 + 1;

翻了很多题解,只能看懂这种解法,够直观够暴力。

class Solution {

public:

vector restoreIpAddresses(string s) {

vector res;

for (int a = 1; a < 4; a ++ )

for (int b = 1; b < 4; b ++ )

for (int c = 1; c < 4; c ++ )

for (int d = 1; d < 4; d ++ ) //abcd分别表示四段ip地址长度

{

if (a + b + c + d == s.size()) //四段长度刚好

{

string s1 = s.substr(0, a); //分别截取四段ip地址

string s2 = s.substr(a, b);

string s3 = s.substr(a + b, c);

string s4 = s.substr(a + b + c);

if (check(s1) && check(s2) && check(s3) && check(s4))

{

string ip = s1 + '.' + s2 + '.' + s3 + '.' + s4;

res.push_back(ip);

}

}

}

return res;