shard tech decision

本文探讨了数据库性能优化的各种策略,包括使用HiveDB Plus Dual Master、缓存技术、索引管理、数据类型选择等,旨在帮助读者理解如何通过合理配置提升数据库系统的运行效率。
We recommend HiveDB plus Dual Master for most installations
Hibernate shard.

local cache with ehcache.
distributed cache with memcached.

  15、使用索引的缺点
  1)减慢增删改数据的速度;
  2)占用磁盘空间;
  3)增加查询优化器的负担;
  当查询优化器生成执行计划时,会考虑索引,太多的索引会给查询优化器增加工作量,导致无法选择最优的查询方案;
  
  16、分析索引效率
  方法:在一般的SQL语句前加上explain;
  分析结果的含义:
  1)table:表名;
  2)type:连接的类型,(ALL/Range/Ref)。其中ref是最理想的;
  3)possible_keys:查询可以利用的索引名;
  4)key:实际使用的索引;
  5)key_len:索引中被使用部分的长度(字节);
  6)ref:显示列名字或者"const"(不明白什么意思);
  7)rows:显示MySQL认为在找到正确结果之前必须扫描的行数;
  8)extra:MySQL的建议;
  
  17、使用较短的定长列
  1)尽可能使用较短的数据类型;
  2)尽可能使用定长数据类型;
  a)用char代替varchar,固定长度的数据处理比变长的快些;
  b)对于频繁修改的表,磁盘容易形成碎片,从而影响数据库的整体性能;
  c)万一出现数据表崩溃,使用固定长度数据行的表更容易重新构造。使用固定长度的数据行,每个记录的开始位置都是固定记录长度的倍数,可以很容易被检测到,但是使用可变长度的数据行就不一定了;
  d)对于MyISAM类型的数据表,虽然转换成固定长度的数据列可以提高性能,但是占据的空间也大;
  
  18、使用not null和enum
  尽量将列定义为not null,这样可使数据的出来更快,所需的空间更少,而且在查询时,MySQL不需要检查是否存在特例,即null值,从而优化查询;
  如果一列只含有有限数目的特定值,如性别,是否有效或者入学年份等,在这种情况下应该考虑将其转换为enum列的值,MySQL处理的更快,因为所有的en

um值在系统内都是以标识数值来表示的;


If you do store UUID values, you should remove the dashes or, even better, convert
the UUID values to 16-byte numbers with UNHEX( ) and store them in a
BINARY(16) column. You can retrieve the values in hexadecimal format with the
HEX( ) function.
Values generated by UUID( ) have different characteristics from those generated
by a cryptographic hash function such ash SHA1( ): the UUID values are unevenly
distributed and are somewhat sequential. They’re still not as good as a monotonically
increasing integer, though.

use Hash indexes when possible

Isolate the Column

If you’re using InnoDB and don’t need any particular clustering, it can be a good idea
to define a surrogate key, which is a primary key whose value is not derived from
your application’s data. The easiest way to do this is usually with an AUTO_INCREMENT
column. This will ensure that rows are inserted in sequential order and will offer better
performance for joins using primary keys.
It is best to avoid random (nonsequential) clustered keys. For example, using UUID
values is a poor choice from a performance standpoint: it makes clustered index
insertion random, which is a worst-case scenario, and does not give you any helpful
data clustering.
To demonstrate,

Where possible, try to extend existing indexes rather than adding new ones. It is usually
more efficient to maintain one multicolumn index than several single-column
indexes. If you don’t yet know your query distribution, strive to make your indexes as
selective as you can, because highly selective indexes are usually more beneficial

multicolumn index than several single-column
As a summary: Use multi column indexes is typically best idea if you use AND between such columns in where clause. Index merge does helps performance but it is far from performance of combined index in this case. In case you're using OR between columns - single column indexes are required for index merge to work and combined indexes can't be used for such queries

high performance

You may have noticed that we’re keeping the age column at the end of the index.

Avoiding Multiple Range Conditions

We can add special indexes for sorting these low-selectivity cases. For example, an
index on (sex,rating) can be used for the following query:
mysql> SELECT <cols> FROM profiles WHERE sex='M' ORDER BY rating LIMIT 10;

Clustering by primary key
All InnoDB tables are clustered by the primary key, which you can use to your
advantage in schema design.
All indexes contain the primary key columns
so if you don’t keep your primary
key short, the indexes will grow very large.

Slow data load
As of MySQL 5.0, InnoDB does not specially optimize data load operations. It
builds indexes a row at a time, instead of building them by sorting. This may
result in significantly slower data loads.

Foreign keys aren’t free.
Instead of using foreign keys as constraints, it’s often a good idea to constrain the
values in the application.
03-16
### 关于 Shard1 的分片机制、配置及错误处理 Shard 是分布式数据库或搜索引擎中的一个重要概念,用于实现数据的水平分区存储。以下是关于 shard1 分片机制、配置以及可能遇到的相关问题及其解决方案。 #### 一、Shard1 的分片机制 在 Elasticsearch 中,shard 是其核心的数据分布单元。当创建索引时,默认会分配主分片 (primary shard) 和副本分片 (replica shard)[^5]。 - 主分片负责存储实际数据并处理写操作。 - 副本分片则提供冗余备份和读取扩展能力。 对于 shard1 而言,它通常是指某个特定的主分片编号为 1 的实例。Elasticsearch 使用一致性哈希算法来决定文档应该存放在哪个 shard 上[^6]。具体计算方式如下: ```python hash(_id) % number_of_primary_shards ``` 如果 `number_of_primary_shards` 设置不当,则可能导致负载不均衡等问题。 #### 二、Shard1 的配置注意事项 为了优化性能,在配置 shard1 或其他 shards 时需要注意以下几点: 1. **合理设置主分片数量**:建议根据硬件资源(CPU 核心数、内存大小等)调整主分片数目。过多或者过少都会影响查询效率[^7]。 2. **启用自动管理功能**:通过修改 elasticsearch.yml 文件可以开启一些高级特性,比如 shard allocation awareness[^8]。 ```yaml cluster.routing.allocation.awareness.attributes: zone ``` 3. **监控健康状态**:定期检查 `_cat/shards?v=true&h=index,shard,prirep,state,unassigned.reason` API 输出结果,确认是否存在未分配(unassigned)的情况[^9]。 #### 三、常见错误处理方案 针对 shard1 可能发生的异常情况列举如下几种典型场景及对应措施: ##### 场景 A - 数据丢失风险 原因可能是磁盘损坏或是节点突然离线造成的 primary shard 不可用。此时可以通过重建 replica 来恢复服务正常运行[^10]。 ##### 场景 B - Guava 版本冲突引发 NoSuchMethodError 类似于上述提到过的 Java 运行时报错信息 `java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()`[^3] ,这通常是由于依赖库版本不同步引起的兼容性问题所致。解决办法是统一项目所使用的 guava jar 包版本号。 ##### 场景 C - 日志路径不可访问 假如 Spark 将日志上传至 HDFS 后无法被 JobHistory Server 正确解析显示出来的话,按照之前给出的经验提示需要确保 yarn-site.xml 配置项正确指向目标地址 `http://<host>:19888/jobhistory/logs` 并重启相关组件完成同步更新[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值