1. 问题:分表的自增主键重复
现象:
对point_msg_his和broadcast_msg_his表进行了分表操作,该表有自增主键id,分表键使用的是其他字段。
在sqlmap中的insert语句:
<!--table insert--> <insert id="insert" parameterClass="pointMsgHisDO" > insert into point_msg_his ( id, receive_id, template_id, app_id, app_channel, content, status, memo, gmt_created, gmt_modified ) values ( #id#, #receiveId#, #templateId#, #appId#, #appChannel#, #content#, #status#, #memo#, now(), now() ) <selectKey resultClass="long" keyProperty="id"> <![CDATA[SELECT LAST_INSERT_ID() AS ID ]]> </selectKey> </insert>
插入的时候,出现了多张分表的id重复,全局sequence表中message_his表的全局自增主键,没有增加。
原因:
在插入数据时,如果在字段列表中指定自增字段,且在values 列表中没有指定以next value for MYCATSEQ_POINT_MSG_HIS 取值,则会使用数据表本身的自增规则,导致主键重复
解决方案:
方案-1 [推荐使用]:在插入数据的代码中,删除列表字段 id 以及对应的 values;
<!--table insert--> <insert id="insert" parameterClass="pointMsgHisDO" > insert into point_msg_his ( receive_id, template_id, app_id, app_channel, content, status, memo, gmt_created, gmt_modified ) values ( #receiveId#, #templateId#, #appId#, #appChannel#, #content#, #status#, #memo#, now(), now() ) <selectKey resultClass="long" keyProperty="id"> <![CDATA[SELECT LAST_INSERT_ID() AS ID ]]> </selectKey> </insert>
方案-02:使用 insert into point_msg_his(id,####) values(next value for MYCATSEQ_POINT_MSG_HIS ,###)
<!--table insert--> <insert id="insert" parameterClass="pointMsgHisDO" > insert into point_msg_his ( id, receive_id, template_id, app_id, app_channel, content, status, memo, gmt_created, gmt_modified ) values ( next value for MYCATSEQ_POINT_MSG_HIS, #receiveId#, #templateId#, #appId#, #appChannel#, #content#, #status#, #memo#, now(), now() ) <selectKey resultClass="long" keyProperty="id"> <![CDATA[SELECT LAST_INSERT_ID() AS ID ]]> </selectKey> </insert>
2. 问题:SQL语句查询结果比实际条数少
现象:
在sql语句没有加limit关键字,且符合条件的条目大于100个时,查询结果只返回了100条数据。
sql语句:
select * from table where xxx=xxx
因为涉及到所有查询语句,所以在使用MyCat后,每条语句都要经过审核。
原因:
MyCAT为了限制不带任何过过滤条件的查询(尤其是大表),给服务器带来的额外压力,默认返回100行数据。
参数配置 schema.xml
<schema name="push_center" checkSQLschema="false" sqlMaxLimit="100">
方案:
已经将MyCat默认limit改为了1000.
方案-01:应用层面,非主键查询,必须先对总数进行查询(就是先count),然后可以选择分页查询,或者在语句中加入 limit关键字。
PS: 当查询SQL中加入 limit 限制之后,返回结果以语句中限制为准。对大表的分页查询,请提交DBA审核。