create table `tb_system_category` (
`id` int(11) not null auto_increment,
`c_parent_id` int(11) not null,
`c_name` varchar(50) not null,
`c_full_name` varchar(200) default null,
`c_code` varchar(50) not null,
`c_describe` text,
primary key (`id`)
) engine=innodb auto_increment=123 default charset=utf8;
`id` int(11) not null auto_increment,
`c_name` varchar(255) not null,
`c_package_name` varchar(255) not null,
`c_category_code` varchar(50) not null default '0',
primary key (`id`)
) engine=innodb auto_increment=138583 default charset=utf8;
筆者先是使用sql聯表語句進行查詢,不外乎left join等語句的使用,結果發現執行速度巨慢無比,遂轉成儲存過程實現,儲存過程**如下所示:
begin
declare crand varchar(50) default rand();
-- 建立臨時表
create temporary table if not exists tb_system_temp_wac(
id integer,
c_name varchar(255),
c_package_name varchar(255),
c_category_code varchar(50),
categoryname varchar(255),
crand varchar(50)
); call generatecategoryname(crand);
-- 返回結果
set @mysql = concat('select * from tb_system_temp_wac where crand=\'',crand,'\'');
prepare stmt from @mysql;
execute stmt;
endbegin
declare sqlstr varchar(4000);
if ctype = 'count' then
else
end if;
if length(cname) > 0 then
set sqlstr = concat(sqlstr,'and c_name like \'%',cname,'%\' ');
end if;
if length(pname) > 0 then
set sqlstr = concat(sqlstr,'and c_package_name like \'%',pname,'%\' ');
end if;
if cid > 0 then
set sqlstr = concat(sqlstr,'and id=',cid,' ');
end if;
if length(ccategorycodes) > 0 then
set sqlstr = concat(sqlstr,'and c_category_code in(',ccategorycodes,') ');
end if;
if ctype != 'count' then
set sqlstr = concat('insert into tb_system_temp_wac(id,c_name,c_package_name,c_category_code,categoryname,crand) ',sqlstr,'limit ',startrow,',',pagesize);
end if;
set @mysql = sqlstr;
prepare stmt from @mysql;
execute stmt;
end
create procedure generatecategoryname(in c_rand varchar(50))
begin
declare finished integer default 0;
declare thiscategoryname varchar(20);
declare cur cursor for select categoryname from tb_system_temp_wac where crand = c_rand;
declare continue handler for not found set finished = 1;
open cur;
fetch cur into thiscategoryname;
while finished = 0 do
set @mysql = concat('update tb_system_temp_wac set categoryname=(select c_full_name from tb_system_category where c_code=\'',thiscategoryname,'\' limit 0,1) where c_category_code=\'',thiscategoryname,'\' and crand=\'',c_rand,'\'');
prepare stmt from @mysql;
execute stmt;
fetch cur into thiscategoryname;
end while;
end
MySQL儲存過程,樹狀結構資料查詢
1.根據父級節點id查詢其所有的子級節點id,查詢結果中包含當前傳入的父級節點id值 根據傳入的父級id查詢所有子節點的id tablename 表名 idkey 節點標識 pidkey 父節點標識 pid 父節點id值 呼叫示例 call get child nodes org id pid 21...
SQL分頁儲存過程原始碼 百萬資料查詢毫秒不到
tbname nvarchar 255 要分頁顯示的表名 注意 此處可以是表名,視,函式名 fieldkey nvarchar 255 用於定位記錄的主鍵 惟一鍵 字段,只能是單個字段 pagecurrent int 1,要顯示的頁碼 pagesize int 10,每頁的大小 記錄數 fields...
海量 千萬級 資料查詢分頁 獲取指定頁 儲存過程
海量 千萬級 資料查詢獲取指定頁 create procedure proc pagination tblname varchar 255 表名 strgetfields varchar 1000 需要返回的列 fldname varchar 255 排序的欄位名 pagesize int 10,頁...