#最近读的东西

#1

Mysql 官方文档

With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag to the mysql_real_connect() C API function when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.

在用upsert语句时,标准的返回值是 1 插入、2 更新、0 无影响。

In assignment value expressions in the ON DUPLICATE KEY UPDATE clause, you can use the VALUES(col_name) function to refer to column values from the INSERT portion of the INSERT … ON DUPLICATE KEY UPDATE statement. In other words, VALUES(col_name) in the ON DUPLICATE KEY UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred.

VALUES() 在上述语句的后面的时候,语法从来没见过,是存在歧义的,是代指数据库里的值还是插入语句中的值。

1
2
3
4
5
6
7
8
9
INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);

-- 等价于:

INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=3;
INSERT INTO t1 (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=9;

An INSERT … ON DUPLICATE KEY UPDATE statement against a table having more than one unique or primary key is also marked as unsafe. (Bug #11765650, Bug #58637)

当一个表中有多个唯一索引时,同时使用upsert的结果是不确定的,只会更新随机一个。

#2

在开发时经常会出现 int 与 string 相互转换的场景,所以从int64转换成interface{}的时候,到底发生了什么 👆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import (
"strconv"
"testing"
)

func BenchmarkInterfaceConversion(b *testing.B) {
num := int64(123)
var result int64

b.ResetTimer()
for i := 0; i < b.N; i++ {
interfaceNum := interface{}(num)
result = interfaceNum.(int64)
}

_ = result
}

func BenchmarkStrconvConversion(b *testing.B) {
num := int64(123)
var result int64

b.ResetTimer()
for i := 0; i < b.N; i++ {
str := strconv.FormatInt(num, 10)
result, _ = strconv.ParseInt(str, 10, 64)
}

_ = result
}
1
2
3
4
5
6
7
8
 [2023/11/03 13:11:15] mbp ➜  ~/Downloads/workspace/ct  go test -bench=. conv_test.go
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkInterfaceConversion-8 1000000000 0.4254 ns/op
BenchmarkStrconvConversion-8 23592367 52.78 ns/op
PASS
ok command-line-arguments 2.849s

#3

为什么对 map 的访问,接了一个参数就不会 panic,还没找到资料

1
2
3
4
5
6
7
8
9
10
11
12
package main

import (
"fmt"
"testing"
)

func TestHelloWorld(t *testing.T) {
var m map[string]string
v, ok := m["o"]
fmt.Printf("value:%+v ok:%+v\n", v, ok)
}
1
2
3
4
5
6
 [2023/11/03 13:21:49] mbp ➜  ~/Downloads/workspace/ct  go test -v -run . map_test.go
=== RUN TestHelloWorld
value: ok:false
--- PASS: TestHelloWorld (0.00s)
PASS
ok command-line-arguments 0.290s