MySQL存储过程导致问题?

时间:2009-12-29 14:30:36

标签: stored-procedures mysql

编辑:

我已将我的mysql等待超时缩小到这一行:

    IF @resultsFound > 0 THEN
        INSERT INTO product_search_query (QueryText, CategoryId) VALUES (keywords, topLevelCategoryId);
    END IF;

知道这会导致问题的原因吗?我无法解决这个问题!

大家好,我写了一个存储过程来搜索某些类别的产品,由于我遇到的某些限制,我无法做我想要的(限制,但仍然返回找到的总行数) ,排序等。)

这意味着拆分一串类别ID,从1,2,3 in到临时表,然后根据排序选项和限制构建全文搜索查询,执行查询字符串,然后选择总数结果数量。

现在,我知道我不是MySQL大师,离它很远,我已经有了它的工作,但我一直在等待产品搜索等等。所以我想这可能会导致某种形式的问题

有没有人有任何想法我可以整理它,甚至以一种我可能不知道的更好的方式做到这一点?

谢谢..

DELIMITER $$

DROP PROCEDURE IF EXISTS `product_search` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `product_search`(keywords text, categories text, topLevelCategoryId int, sortOrder int, startOffset int, itemsToReturn int)
BEGIN

declare foundPos tinyint unsigned;
declare tmpTxt text;
declare delimLen tinyint unsigned;
declare element text;
declare resultingNum int unsigned;

drop temporary table if exists categoryIds;
create temporary table categoryIds
(
`CategoryId` int
) engine = memory;


set tmpTxt = categories;

set foundPos = instr(tmpTxt, ',');
while foundPos <> 0 do
set element = substring(tmpTxt, 1, foundPos-1);
set tmpTxt = substring(tmpTxt, foundPos+1);
set resultingNum = cast(trim(element) as unsigned);

insert into categoryIds (`CategoryId`) values (resultingNum);

set foundPos = instr(tmpTxt,',');
end while;

if tmpTxt <> '' then
insert into categoryIds (`CategoryId`) values (tmpTxt);
end if;

CASE
  WHEN sortOrder = 0 THEN
    SET @sortString = "ProductResult_Relevance DESC";
  WHEN sortOrder = 1 THEN
    SET @sortString = "ProductResult_Price ASC";
  WHEN sortOrder = 2 THEN
    SET @sortString = "ProductResult_Price DESC";
  WHEN sortOrder = 3 THEN
    SET @sortString = "ProductResult_StockStatus ASC";
END CASE;

SET @theSelect = CONCAT(CONCAT("
    SELECT SQL_CALC_FOUND_ROWS
      supplier.SupplierId as Supplier_SupplierId,
      supplier.Name as Supplier_Name,
      supplier.ImageName as Supplier_ImageName,

      product_result.ProductId as ProductResult_ProductId,
      product_result.SupplierId as ProductResult_SupplierId,
      product_result.Name as ProductResult_Name,
      product_result.Description as ProductResult_Description,
      product_result.ThumbnailUrl as ProductResult_ThumbnailUrl,
      product_result.Price as ProductResult_Price,
      product_result.DeliveryPrice as ProductResult_DeliveryPrice,
      product_result.StockStatus as ProductResult_StockStatus,
      product_result.TrackUrl as ProductResult_TrackUrl,
      product_result.LastUpdated as ProductResult_LastUpdated,

      MATCH(product_result.Name) AGAINST(?) AS ProductResult_Relevance
    FROM
      product_latest_state product_result
    JOIN
      supplier ON product_result.SupplierId = supplier.SupplierId
    JOIN
      category_product ON product_result.ProductId = category_product.ProductId
    WHERE
      MATCH(product_result.Name) AGAINST (?)
    AND
      category_product.CategoryId IN (select CategoryId from categoryIds)
    ORDER BY
      ", @sortString), "
    LIMIT ?, ?;
  ");

    set @keywords = keywords;
    set @startOffset = startOffset;
    set @itemsToReturn = itemsToReturn;

    PREPARE TheSelect FROM @theSelect;
    EXECUTE TheSelect USING @keywords, @keywords, @startOffset, @itemsToReturn;

    SET @resultsFound = FOUND_ROWS();

    SELECT @resultsFound as 'TotalResults';

    IF @resultsFound > 0 THEN
        INSERT INTO product_search_query (QueryText, CategoryId) VALUES (keywords, topLevelCategoryId);
    END IF;

END $$

DELIMITER ;

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:4)

您无法使用此查询。

试试这个:

  1. PRIMARY KEY

    上创建categoryIds (categoryId)
    • 确保supplier (supplied_id)PRIMARY KEY

    • 确保category_product (ProductID, CategoryID)(按此顺序)为PRIMARY KEY,或者您的索引为ProductID

  2. <强>更新

    如果导致问题的INSERTproduct_search_query表中的MyISAM,问题可能是MyISAM锁定。

    如果

    MyISAM决定在表格中间的空闲块中插入一行可能导致超时,则会锁定整个表格。

    请尝试使用INSERT DELAYED

    IF @resultsFound > 0 THEN
        INSERT DELAYED INTO product_search_query (QueryText, CategoryId) VALUES (keywords, topLevelCategoryId);
    END IF;
    

    这会将记录放入插入队列并立即返回。该记录将在以后异步添加。

    请注意,如果服务器在发出命令后但在实际插入记录之前死亡,则可能会丢失信息。

    <强>更新

    由于您的表格为InnoDB,因此表格锁定可能存在问题。 INSERT DELAYED不支持InnoDB

    根据查询的性质,DML表上的InnoDB查询可能会设置间隙锁,这会锁定插入内容。

    例如:

    CREATE TABLE t_lock (id INT NOT NULL PRIMARY KEY, val INT NOT NULL) ENGINE=InnoDB;
    INSERT
    INTO    t_lock
    VALUES
            (1, 1),
            (2, 2);
    

    此查询执行ref次扫描并将锁定放在各个记录上:

    -- Session 1
    START TRANSACTION;
    UPDATE  t_lock
    SET     val = 3
    WHERE   id IN (1, 2)
    
    -- Session 2
    START TRANSACTION;
    INSERT
    INTO    t_lock 
    VALUES  (3, 3)
    -- Success
    

    此查询在执行相同操作时执行range扫描,并在键值2后放置一个间隙锁定,这将不允许插入键值3

    -- Session 1
    START TRANSACTION;
    UPDATE  t_lock
    SET     val = 3
    WHERE   id BETWEEN 1 AND 2
    
    -- Session 2
    START TRANSACTION;
    INSERT
    INTO    t_lock 
    VALUES  (3, 3)
    -- Locks
    

答案 1 :(得分:0)

启用慢速查询,这样可以让您了解执行时间过长会导致超时。

http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html

选择最慢的查询并对其进行优化。然后运行一段时间并重复。

这里有一些很好的信息和工具http://hackmysql.com/nontech

DC

更新:

要么你有一个导致超时的网络问题,如果你使用的是本地mysql实例那么这是不太可能的,或者有些东西锁定表太长导致超时。锁定一个或多个表太长的进程将作为慢速查询列在慢速日志中。您还可以获取慢速日志查询以显示未使用索引的任何查询,从而导致查询效率低下。

如果您在出现问题时可以解决问题,那么您也可以使用phpmyadmin或命令行之类的工具来运行“SHOW PROCESSLIST \ G”,这将为您提供一个列表,列出正在运行的查询发生。

您认为问题出在您的insert语句中,因此有些东西会锁定该表。因此,您需要找到锁定该表的内容,因此您需要找到运行速度太慢以至于锁定表太长时间。慢查询是一种方法。

其他要注意的事项

CPU - 闲置或全速运行

IO - 导致劫持

RAM - 你一直在交换(会导致过多的io)

表product_search_query是否使用索引?

什么是主键?

如果您的索引使用太长的字符串?你可能会构建一个巨大的索引文件,导致非常慢的插入(慢查询日志也会显示)

是的,问题可能在其他地方,但你必须从某个地方开始,不是你。

DC

答案 2 :(得分:0)

尝试使用以下内容包装EXECUTE:

SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

执行选择使用@keywords,@ keywords,@ startOffset,@ itemToReturn;

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

我在TSQL中为所有报告存储的proc和搜索做了类似的事情,其中​​可重复读取对于减少与数据库上运行的其他进程的锁定/阻塞问题并不重要。

相关问题