- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章通过实例认识MySQL中前缀索引的用法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
今天在测试环境中加一个索引时候发现一警告 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
root@test 07:57:52>
alter
table
article
drop
index
ind_article_url;
Query OK, 144384
rows
affected (16.29 sec)
Records: 144384 Duplicates: 0 Warnings: 0
root@test 07:58:40>
alter
table
article
add
index
ind_article_url(url);
Query OK, 144384
rows
affected, 1 warning (19.52 sec)
Records: 144384 Duplicates: 0 Warnings: 0
root@test 07:59:23>show warnings;
+———+——+———————————————————+
|
Level
| Code | Message |
+———+——+———————————————————+
| Warning | 1071 | Specified
key
was too long;
max
key
length
is
767 bytes |
+———+——+———————————————————+
1 row
in
set
(0.00 sec)
|
用show create table article查看索引以及表结构的信息:
1
2
3
4
5
6
7
8
|
`URL`
varchar
(512)
default
NULL
COMMENT ‘外链url',
……
KEY
`ind_article_url` (`URL`(383))
…..
DEFAULT
CHARSET=gbk
……
drop
table
test;
create
table
test(test
varchar
(767)
primary
key
)charset=latin5;
|
– 成功 接下来未测试,在不同的字符集:
1
2
|
drop
table
test;
create
table
test(test
varchar
(768)
primary
key
)charset=latin5;
|
– 错误 – 。
1
2
3
|
ERROR 1071 (42000): Specified
key
was too long;
max
key
length
is
767 bytes
drop
table
test;
create
table
test(test
varchar
(383)
primary
key
)charset=GBK;
|
– 成功 。
1
2
|
drop
table
test;
create
table
test(test
varchar
(384)
primary
key
)charset=GBK;
|
– 错误 – 。
1
2
3
|
ERROR 1071 (42000): Specified
key
was too long;
max
key
length
is
767 bytes
drop
table
test;
create
table
test(test
varchar
(255)
primary
key
)charset=UTF8;
|
– 成功 。
1
2
|
drop
table
test;
create
table
test(test
varchar
(256)
primary
key
)charset=UTF8;
|
– 错误 – 。
1
|
ERROR 1071 (42000): Specified
key
was too long;
max
key
length
is
767 bytes
|
MySQL的varchar索引只支持不超过768个字节 或者 768/2=384个双字节 或者 768/3=256个三字节的字段 而 GBK是双字节的,UTF-8是三字节的。 那么上面出现的原因就明了,我的字符集是为GBK为双字节,而url为512个字符,1024个字节,所以超过字符串索引的限制,报出了警告,mysql默认创建了383(766字节)长度的前缀索引。 我们知道小的索引大小不仅对空间存储,内存的降低和性能的提升有重大作用,那么在计算前缀索引的长度的时候,需要我们做出明智的选择,怎么明智? 全索引列的选择性:
1
2
3
4
5
6
|
root@test 08:10:35>
select
count
(
distinct
(url))/
count
(*)
from
article;
+——————————-+
|
count
(
distinct
(url))/
count
(*) |
+——————————-+
| 0.0750 |
+——————————-+
|
对各种长度的前缀列计算其选择性:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
root@test 08:16:41>
select
count
(
distinct
left
(url,76))/
count
(*) url_76,
->
count
(
distinct
left
(url,77))/
count
(*) url_77,
->
count
(
distinct
left
(url,78))/
count
(*) url_78,
->
count
(
distinct
left
(url,79))/
count
(*) url_79,
->
count
(
distinct
left
(url,80))/
count
(*) url_80,
->
count
(
distinct
left
(url,81))/
count
(*) url_81,
->
count
(
distinct
left
(url,82))/
count
(*) url_82,
->
count
(
distinct
left
(url,83))/
count
(*) url_83,
->
count
(
distinct
left
(url,84))/
count
(*) url_84,
->
count
(
distinct
left
(url,85))/
count
(*) url_85
->
from
article;
+——–+——–+——–+——–+——–+——–+——–+——–+——–+——–+
| url_76 | url_77 | url_78 | url_79 | url_80 | url_81 | url_82 | url_83 | url_84 | url_85 |
+——–+——–+——–+——–+——–+——–+——–+——–+——–+——–+
| 0.0747 | 0.0748 | 0.0749 | 0.0749 | 0.0749 | 0.0749 | 0.0749 | 0.0749 | 0.0749 | 0.0750 |
+——–+——–+——–+——–+——–+——–+——–+——–+——–+——–+
1 row
in
set
(1.82 sec)
|
我们看到选择85的长度的时候,该前缀列的选择性和全列的选择性相当了: alter table article add index ind_article_url(url(85)),而不必选择383个字节作为前缀; 但是前缀索引还是有一点不足的地方,就是在查询语句中order by 和group by不能使用到前缀索引 。
1
2
3
4
5
6
7
|
root@test 08:49:24>explain
select
id,url,deleted
from
article
group
by
url;
+—-+————-+————-+——+—————+——+———+——+——–+———————————+
| id | select_type |
table
| type | possible_keys |
key
| key_len | ref |
rows
| Extra |
+—-+————-+————-+——+—————+——+———+——+——–+———————————+
| 1 | SIMPLE | article |
ALL
|
NULL
|
NULL
|
NULL
|
NULL
| 139844 | Using
temporary
; Using filesort |
+—-+————-+————-+——+—————+——+———+——+——–+———————————+
1 row
in
set
(0.00 sec);
|
。
最后此篇关于通过实例认识MySQL中前缀索引的用法的文章就讲到这里了,如果你想了解更多关于通过实例认识MySQL中前缀索引的用法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
发布以下查询时,出现错误响应 {"error":{"root_cause":[{"type":"parsing_exception","reason":"[prefix] query does not
我对 Python 和 Django 真的很陌生......我想做的是: 在 Mac OS 10.6.8 上安装 Python 2.7 安装 pip 安装 Django 安装 virtualenvwr
前缀表达式 前缀表达式又称波兰式,前缀表达式的运算符位于操作数之前。 例如: ( 3 + 4 ) × 5 − 6 (3+4)×5-6(3+4)×5−6 对应的前缀表达式就是 - × + 3 4 5 6
如何在Intel C编译器中定义俄语字符串? 在MSVS 2008中,我这样做: _wsetlocale(LC_ALL, L"Russian"); wprintf(L"текст"); 而且有效。 在
这是我到目前为止所得到的: SPECS = $(shell find spec -iname "*_spec.js") spec: @NODE_ENV=test \ @NODE_PAT
我看到了下面的前缀::它代表什么? :abc 是一个关键字,但是 ::abc 是什么? 谢谢,穆尔塔扎 最佳答案 假设当前命名空间是my.app。然后, ::x 是 :my.app/x 的阅读器简写,
我为我的 discord 创建了一个建议功能,用户可以说 +suggest(建议),它会自动发布到另一个 channel 。 有些事情我需要帮助: 将“建议由用户制作”放入标题中,而不是在单独的行中。
#include int main() { int a=1; printf("%d",(++a)++); return 0; } 此代码出现错误 error: invalid lvalue in
我在使用前缀和后缀运算符对数字执行减法时遇到了一个小问题。这是我的程序: public class postfixprefix { public static void main (Strin
当我在 Android native 浏览器中运行 HTML5 兼容性测试时,它会看到 IndexedDB 支持标记为“Prefixed”,而在 Chrome 和其他浏览器中则标记为“Yes”。我知道
我试过重载运算符--前缀,但我有错误,有人帮忙吗? #include #include "Circulo.h" using namespace std; int main() { //par
我正在尝试在我正在制作的这个论坛上创建一个引用功能,当我按下引用时,我只需用 Markdown 填充 textarea ,但唯一的事情是我需要在每行的 markdown 前面加上 > 前缀,这样它就是
friend 之间打赌。sum 变量定义为全局变量。我们有 2 个线程在循环 1..100 上运行并在每个循环中将 sum 递增 1。 打印什么?“和=”? int sum = 0; void fun
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Post Increment and Pre Increment concept? 谁能明确解释前缀增量与后
从模板类继承时,我需要在派生类中访问的所有基类成员前面加上this: template struct X{ int foo; void bar(); }; template struct
据我所知,在 C++ 中,在同一类的函数成员中调用另一个成员函数不需要“this”前缀,因为它是隐式的。但是,在使用函数指针的特定情况下,编译器需要它。仅当我通过 func 指针为调用包含“this”
例如,考虑以下名称冲突的地方 nest1 : template class nest1 {}; class cls { public: template class nest1 {};
我无法理解下面一段特定代码的逻辑。 int i[] = { 21, 4, -17, 45 }; int* i_ptr = i; std::cout << (*i_ptr)++ << std::endl
有人能给我指出正确的方向吗,我目前有一个可搜索的数据库,但遇到了按标题搜索的问题。 如果标题以“The”开头,那么显然标题将位于“T”部分,避免搜索“The”的好方法是什么?我应该连接两个字段来显示标
我在 2 小时前创建了一个新项目。以与我的旧(不同)项目相同的方式配置它,一切正常。 在我的 podfile 中我有: pod 'CocoaLumberjack', '2.0.0-rc2' 如果我在
我是一名优秀的程序员,十分优秀!