写一个程序找出两个单向链表的交叉起始点,可能是我英语不好,图里画的其实还有一点是交叉以后所有节点都是相同的 Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
begin to intersect at node c1.
Example 1:
1 2 3
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 Output: Reference of the node with value = 8 Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
delimiter $ #以delimiter来标记用$表示存储过程结束 createprocedure nullIndex1() begin declare i int; declare j int; set i=1; set j=1; while(i<=100) do while(j<=100) do IF (i %3=0) THEN INSERTINTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (null , LEFT(MD5(RAND()), 8), LEFT(MD5(RAND()), 8)); ELSEIF (i %3=1) THEN INSERTINTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (LEFT(MD5(RAND()), 8), NULL, LEFT(MD5(RAND()), 8)); ELSE INSERTINTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (LEFT(MD5(RAND()), 8), LEFT(MD5(RAND()), 8), NULL); END IF; set j=j+1; end while; set i=i+1; set j=1; end while; end $ call nullIndex1();
然后看下我们的 is null 查询
1
EXPLAIN select*from null_index_t WHERE null_key isnull;
再来看看另一个
1
EXPLAIN select*from null_index_t WHERE null_key isnotnull;
从这里能看出来啥呢,可以思考下
从上面可以发现,is null应该是用上了索引了,所以至少不是一刀切不能用,但是看着is not null好像不太行额 我们在做一点小改动,把这个表里的数据改成 9100 条是 null,剩下 900 条是有值的,然后再执行下
然后再来看看执行结果
1
EXPLAIN select*from null_index_t WHERE null_key isnull;
1
EXPLAIN select*from null_index_t WHERE null_key isnotnull;
是不是不一样了,这里再补充下我试验使用的 mysql 是 5.7 的,不保证在其他版本的一致性, 其实可以看出随着数据量的变化,mysql 会不会使用索引是会变化的,不是说 is not null 一定会使用,也不是一定不会使用,而是优化器会根据查询成本做个预判,这个预判尽可能会减小查询成本,主要包括回表啥的,但是也不一定完全准确。