日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

關(guān)于避免在 PL/SQL 中使用嵌套游標(biāo)查詢_Mssql數(shù)據(jù)庫教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:解讀用最簡單的步驟備份SQL數(shù)據(jù)庫的文件到本地
用最簡單的步驟備份sql數(shù)據(jù)庫的文件到本地: 以下為引用的內(nèi)容:   response.Charset = utf-8   Session.Codepage = 65001

考慮下面的 PL/SQL 代碼,這段代碼生成一個 XML 格式的矩陣樣式的報表:

以下為引用的內(nèi)容:

  declare
  l_count   integer;
  begin
  dbms_output.put_line('<matrix>');
  -- generate matrix of parts by country
  for part in (select id,description from parts order by description) loop
  dbms_output.put_line('<row>');
  dbms_output.put_line('<cell>'part.description'</cell>');
  for country in (select code from countries order by name) loop
  select sum(cnt) into l_count from orders
  where part_id = part.id and cc = country.code;
  dbms_output.put_line('<cell>'nvl(l_count,0)'</cell>');
  end loop;
  dbms_output.put_line('</row>');
  end loop;
  dbms_output.put_line('</matrix>');
  end;

假如在這個例子中 parts 和 countries 有很多行數(shù)據(jù),那么性能就會趨于下降。這是因為,在 PL/SQL 中,每次碰到一個游標(biāo) FOR 循環(huán),在重新查詢并獲得數(shù)據(jù)時,都會有一個切換到 SQL 的上下文切換。

以一些服務(wù)器端內(nèi)存為代價,提高這種構(gòu)造的速度是有可能做到的——假如動態(tài)構(gòu)建 PL/SQL 數(shù)據(jù)表和矩陣單元格條目就可以提高速度。例如:

以下為引用的內(nèi)容:

  declare
  type part_tbl_type is table of parts%rowtype index by binary_integer;
  part_tbl   part_tbl_type;
  --
  type country_tbl_type is table of countries%rowtype index by binary_integer;
  country_tbl   country_tbl_type;
  --
  type cell_rec is record
  (
  part_id     orders.part_id%type,
  cc        orders.cc%type,
  cnt        orders.cnt%type
  );
  type cell_tbl_type is table of cell_rec index by binary_integer;
  cell_tbl cell_tbl_type;
  --
  i pls_integer;
  begin
  -- build rows
  for row in (select * from parts order by description) loop
  part_tbl(part_tbl.count 1) := row;
  end loop;
  -- build columns
  for col in (select * from countries order by name) loop
  country_tbl(country_tbl.count 1) := col;
  end loop;
  -- build cells
  for cell in (select part_id,cc,sum(cnt) from orders group by part_id,cc) loop
  cell_tbl(cell_tbl.count 1) := cell;
  end loop;
  dbms_output.put_line('<matrix>');
  -- generate matrix of parts by country
  i := cell_tbl.first;
  for row in part_tbl.first .. part_tbl.last loop
  dbms_output.put_line('<row>');
  dbms_output.put_line('<cell>'part_tbl(row).description'</cell>');
  for col in country_tbl.first .. country_tbl.last loop
  if cell_tbl(i).part_id = part_tbl(row).id
  and cell_tbl(i).cc = country_tbl(col).code
  then
  dbms_output.put_line('<cell>'cell_tbl(i).cnt'</cell>');
  i := i 1;
  else
  dbms_output.put_line('<cell>0</cell>');
  end if;
  end loop;
  dbms_output.put_line('</row>');
  end loop;
  dbms_output.put_line('</matrix>');
  end;

游標(biāo)

游標(biāo)的 FOR 循環(huán)現(xiàn)在是獨立運行的,并且特定記錄、特定字段、特定單元格的數(shù)據(jù)被拷貝到三個 PL/SQL 表中。

然后利用記錄和字段具有特定順序這一事實,將結(jié)果構(gòu)建到一個 PL/SQL 表的矩陣中。由于 GROUP BY 的隱式 SORT/MERGE 操作,單元格具有同樣的順序。單元格查詢已經(jīng)被減少到一個查詢,替代了原來的矩陣每個單元格使用一個查詢。

假如字段的數(shù)目相當(dāng)小,那么我們可以使用 BULK COLLECT 構(gòu)建表。BULK COLLECT 不答應(yīng)表記錄的填充,所以我們就需要為用于這個操作的每一列數(shù)據(jù)創(chuàng)建一個獨立的表。前面的例子可以采用 BULK COLLECT 重寫為另外一種形式。

以下為引用的內(nèi)容:

  declare
  type part_id_tbl_type is table of parts.id%type;
  type part_desc_tbl_type is table of parts.description%type;
  part_id_tbl     part_id_tbl_type;
  part_desc_tbl   part_desc_tbl_type;
  --
  type country_code_tbl_type is table of countries.code%type;
  country_code_tbl   country_code_tbl_type;
  --
  type cell_cnt_tbl_type is table of orders.cnt%type;
  cell_part_id_tbl   part_id_tbl_type;
  cell_country_tbl   country_code_tbl_type;
  cell_cnt_tbl     cell_cnt_tbl_type;
  --
  i pls_integer;
  begin
  -- gather rows
  select id,description
  bulk collect into part_id_tbl,part_desc_tbl
  from parts
  order by description;
  -- gather columns
  select code
  bulk collect into country_code_tbl
  from countries
  order by name;
  -- gather cells
  select part_id,cc,sum(cnt)
  bulk collect into cell_part_id_tbl,cell_country_tbl,cell_cnt_tbl
  from orders
  group by part_id,cc;
  dbms_output.put_line('<matrix>');
  -- generate matrix of parts by country
  i := cell_cnt_tbl.first;
  for row in part_id_tbl.first .. part_id_tbl.last loop
  dbms_output.put_line('<row>');
  dbms_output.put_line('<cell>'part_desc_tbl(row)'</cell>');
  for col in country_code_tbl.first .. country_code_tbl.last loop
  if cell_part_id_tbl(i) = part_id_tbl(row)
  and cell_country_tbl(i) = country_code_tbl(col)
  then
  dbms_output.put_line('<cell>'cell_cnt_tbl(i)'</cell>');
  i := i 1;
  else
  dbms_output.put_line('<cell>0</cell>');
  end if;
  end loop;
  dbms_output.put_line('</row>');
  end loop;
  dbms_output.put_line('</matrix>');

分享:如何在SAN上創(chuàng)建SQL Server群集
Storage area networks(SANs)使大量存儲量連接到服務(wù)器變得毫不費力。SANs對于SQL Server安裝特別有用。企業(yè)數(shù)據(jù)庫不僅僅只需要做大量的存儲,它們還有一些不斷增長存儲的需要。也

來源:模板無憂//所屬分類:Mssql數(shù)據(jù)庫教程/更新時間:2009-09-11
相關(guān)Mssql數(shù)據(jù)庫教程