0

我有一个mysql db,数据表是按照分片规则命名的,也就是说名字有相同的前缀,例如:the_table_,并且后缀有一个数字,所以表名会是the_table_1,the_table_2,the_table_3。当我们要选择一行,我们要从sql select mod(primary_key_id, 64)+1中找到表后缀,分片规则,64是一个基数,也就是说我们有64个表。

现在我们想使用Solr来索引表中的数据,但是solr data-config.xml不支持动态名称的sql查询,那么有什么解决这个问题的建议吗?

诸如此类的事情?

<entity name="audit" 
        query="select id,monitor_type,city,STATUS,is_history,timeout,author,author_uid,author_ip,operator_uid,operator,created_at,description from `main_table`">
        <field column="ID" name="id" />
        <field column="MONITOR_TYPE" name="monitor_type" />
        <field column="CITY" name="city" />
        <field column="STATUS" name="status" />
        <field column="IS_HISTORY" name="is_history" />
        <field column="AUTHOR" name="author" />
        <field column="AUTHOR_UID" name="author_uid" />
        <field column="AUTHOR_IP" name="author_ip" />
        <field column="OPERATOR_UID" name="operator_uid" />
        <field column="OPERATOR" name="operator" />

        <entity name="distribution" query="select mod(${audit.ID}, 256)+1 as table_id">
          <entity name="details" query="select content from detail${distribution.table_id} where id=${audit.ID}">
            <field column="CONTENT" name="content" />
          </entity>
        </entity>
</entity>

4

2 回答 2

1

我会创建一个存储过程来做你想做的事,然后从 Solr 调用它。像这样的东西

CREATE PROCEDURE `GetAuditDetails`(IN auditID INT)
BEGIN
  DECLARE tableID INT DEFAULT 0;
  DECLARE sqlString VARCHAR(100) DEFAULT '';
  SELECT MOD(auditID, 256)+1 INTO tableID;
  SET @sqlString = CONCAT('select content from detail', tableID, ' where id=', auditID );
  PREPARE stmt FROM @sqlString;
  EXECUTE stmt;
END;

<entity name="audit" 
    query="select id,monitor_type,city,STATUS,is_history,timeout,author,author_uid,author_ip,operator_uid,operator,created_at,description from `main_table`">
        <field column="ID" name="id" />
        <field column="MONITOR_TYPE" name="monitor_type" />
        <field column="CITY" name="city" />
        <field column="STATUS" name="status" />
        <field column="IS_HISTORY" name="is_history" />
        <field column="AUTHOR" name="author" />
        <field column="AUTHOR_UID" name="author_uid" />
        <field column="AUTHOR_IP" name="author_ip" />
        <field column="OPERATOR_UID" name="operator_uid" />
        <field column="OPERATOR" name="operator" />

        <entity name="details" query="CALL GetAuditDetails(${audit.ID})">
           <field column="CONTENT" name="content" />
        </entity>
</entity>
于 2012-07-24T16:56:35.263 回答
0

首先,嵌套条目查询中不能使用audit.ID,而是${audit.id},否则会出现异常。二、对于Mysql程序:

CREATE PROCEDURE `GetAuditDetails`(IN auditID INT)
BEGIN
  DECLARE tableID INT DEFAULT 0;
  DECLARE sqlString VARCHAR(100) DEFAULT '';
  SELECT MOD(auditID, 256)+1 INTO tableID;
  SET @sqlString = CONCAT('select content from detail', tableID, ' where id=', auditID );
  PREPARE stmt FROM @sqlString;
  EXECUTE stmt;
END;
于 2012-07-27T08:08:23.090 回答