If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Urgent !!! Sql question used ...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-06, 10:59
gferro gferro is offline
Registered User
 
Join Date: Sep 2006
Posts: 6
Question Urgent !!! Sql question used ...

I have a website that access a DB2 database and execute the sql instruction listed below each time all my website´s pages are loaded. But it is taking a great time to load the site and compromising its performance. I was told to make a view and instead of executing the six select(listed below) I would access the view.

What should I do in order to make it faster ?

String[] query2={
"SELECT A.TIMES_NOME, A.TIMES_ID, count(A.TIMES_ID) FROM TIMES A inner join PRODUTOS B on B.PRODUTOS_TIMEID=A.TIMES_ID group by A.TIMES_NOME, A.TIMES_ID ORDER BY A.TIMES_NOME",

"SELECT A.SELECAO_NOME, A.SELECAO_ID, count(A.SELECAO_ID) FROM SELECAO A inner join PRODUTOS B on B.PRODUTOS_SELECAOID=A.SELECAO_ID group by A.SELECAO_NOME, A.SELECAO_ID ORDER BY A.SELECAO_NOME",

"SELECT A.CLUBE_ESTR_NOME, A.CLUBE_ESTR_ID, count(A.CLUBE_ESTR_ID) FROM CLUBE_ESTR A inner join PRODUTOS B on B.PRODUTOS_CLUBEESTRID=A.CLUBE_ESTR_ID group by A.CLUBE_ESTR_NOME, A.CLUBE_ESTR_ID ORDER BY A.CLUBE_ESTR_NOME",

"SELECT A.PRODUTOS__CATEGORIA, A.PRODUTOS_CATEGORIA_ID, count(A.PRODUTOS_CATEGORIA_ID) FROM CATEGORIAS A inner join PRODUTOS B on B.PRODUTOS_TIPOPRODUTOID=A.PRODUTOS_CATEGORIA_ID group by A.PRODUTOS__CATEGORIA, A.PRODUTOS_CATEGORIA_ID ORDER BY A.PRODUTOS__CATEGORIA",

"SELECT A.MARCAS_NOME, A.MARCAS_ID, count(A.MARCAS_ID) FROM MARCAS A inner join PRODUTOS B on B.PRODUTOS_MARCASID=A.MARCAS_ID group by A.MARCAS_NOME, A.MARCAS_ID ORDER BY A.MARCAS_NOME",

"SELECT A.ESPORTE_NOME, A.ESPORTE_ID, count(A.ESPORTE_ID) FROM ESPORTE A inner join PRODUTOS B on B.PRODUTOS_ESPORTEID=A.ESPORTE_ID group by A.ESPORTE_NOME, A.ESPORTE_ID ORDER BY A.ESPORTE_NOME"
};
Reply With Quote
  #2 (permalink)  
Old 09-01-06, 11:20
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Are proper indexes defined and have you done runstats ?

All these seem reference data ... I would put all these tables into the same tablespace and allocate a bufferpool, as big as practically possible to hold the reference tables.
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 09-01-06, 11:39
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
If those tables are not being updated too frequently I'd think about using MQT's
Reply With Quote
  #4 (permalink)  
Old 09-01-06, 11:43
gferro gferro is offline
Registered User
 
Join Date: Sep 2006
Posts: 6
re

the problem is that it is used frequently.
Reply With Quote
  #5 (permalink)  
Old 09-01-06, 11:45
gferro gferro is offline
Registered User
 
Join Date: Sep 2006
Posts: 6
re

D you think an access to a view or a single table is faster ?
Reply With Quote
  #6 (permalink)  
Old 09-01-06, 13:02
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Doesn't make a difference
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #7 (permalink)  
Old 09-03-06, 02:40
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Creating a view with the COUNT(...) in its definition does not speed up the query. Only a pre-computed and stored "Count" column would help in that respect.
That's exactly what a materialised query table (MQT) does.

Alternatively, you could do it yourself, by creating a new table that stores such a Count column, but then you will need to create "after" triggers (on insert, delete and update) to guarantee consistency of that column.

Depending on the sizes of your tables and on the average number of rows per group, both of these solutions may speed up your query considerably!

Did you try out the MQT solution? It's the simplest of the two since you don't have to change your SELECT queries.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #8 (permalink)  
Old 09-03-06, 11:17
gferro gferro is offline
Registered User
 
Join Date: Sep 2006
Posts: 6
What do you mean saying MQT ?

What do you mean with MQT ?
Thanks a lot for your support
Gustavo
Reply With Quote
  #9 (permalink)  
Old 09-03-06, 11:43
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by gferro
What do you mean with MQT ?
MQTs are "materialised query tables". MQTs are new in DB2 since version 8.
See e.g. http://publib.boulder.ibm.com/infoce...e/c0009318.htm
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #10 (permalink)  
Old 09-05-06, 11:35
gferro gferro is offline
Registered User
 
Join Date: Sep 2006
Posts: 6
I will try MQT....

Thanks...I will try to contact my support area and tell then to use MQT.
The problem with the speed is created by the load of the menu that makes 12 selects to load it !
If the MQT doesn´t work I could I try ?
Regards
Reply With Quote
  #11 (permalink)  
Old 09-06-06, 02:47
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
If your application has 12 separate queries, each of them needs to set up a communication to DB2, which may cost 1 second or so each.

So if it is possible to obtain the same information from DB2 in a single query (which would be rather complex, e.g. containing several table joins) you will gain in total application run time, and possibly even gain in DB2 processor time since in a single query the DB2 optimiser might use more efficient logic in composing the data to be returned than your program now does.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #12 (permalink)  
Old 09-06-06, 11:17
gferro gferro is offline
Registered User
 
Join Date: Sep 2006
Posts: 6
Ok....I´ve already sent your message to my application developer.
But they told me that they figured out that the problem could also be in another area of my code. It could be in the index.jsp page. Could you also check the code for me? Your opinion has been very important for us. Thanks in advance. Our website is www.timesetorcidas.com.br and it´s index.jsp page is:

<td valign="top" align="left" height="685">
<table border="0" cellspacing="0" cellpadding="0" height="656">
<tr>
<td height="665" align="center" width="88%">
<table border="0" cellspacing="0" cellpadding="0" height="502">
<tr>
<%@ include file="menu_lateral.jsp" %>
<td width="85%" valign="top" height="662">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="2%" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<%@ include file="menu_topo.jsp" %>
</td>
</tr>
<tr>
<td><img src="<%=imagemtop%>" width="637" height="71"></td>
</tr>
<%@ include file="menu_pulldown1.jsp" %>
<tr>
<td>
<table width="100%" border=0 cellspacing=0 cellpadding=0>
<tr>
<td>
<%@ include file="login.jsp" %>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="424">
<tr>
<td height="439" align="right" valign="top">
<%--Get the Data Access Bean--%>



<%

if (timeid==null) // se o cliente nao tiver um perfil
{ //1 //2 //3 //4 //5 //6
consulta = "SELECT * FROM (SELECT PRODUTOS_ID, PRODUTOS_DESCRICAO, PRODUTOS_VALORUNITARIO, PRODUTOS_DISPONIVEL,PRODUTOS_NOME,PRODUTOS_DISPONI VEL, PRODUTOS_TIMEID, PRODUTOS_SELECAOID,PRODUTOS_CLUBEESTRID, ROW_NUMBER() OVER (ORDER BY PRODUTOS_ID DESC) AS rownumber from produtos Where produtos_visivel = 'S') as PRODUTOS where rownumber < 50";
}
else //se o cliente jah tiver um perfil
{ //1 //2 //3 //4 //5 //6
consulta = "SELECT * FROM (SELECT PRODUTOS_ID, PRODUTOS_DESCRICAO, PRODUTOS_VALORUNITARIO, PRODUTOS_DISPONIVEL,PRODUTOS_NOME,PRODUTOS_DISPONI VEL, PRODUTOS_TIMEID, PRODUTOS_SELECAOID,PRODUTOS_CLUBEESTRID, ROW_NUMBER() OVER (ORDER BY PRODUTOS_ID DESC) AS rownumber from produtos Where produtos_visivel = 'S' AND PRODUTOS_TIMEID = " + session.getAttribute("timeid") + ") as PRODUTOS where rownumber < 50";
}

Vector hidden_names ;
Vector Ids = new Vector();
Hashtable produtos = new Hashtable();
int contaregistros=0; // retorna o numero de registros da query
float tot=0.00F;
int totpaginas=0 ;
String idnumb =null;

try{
pstmt = connect.prepareStatement(consulta);
resultset = pstmt.executeQuery();

if(resultset.next())
{
do{
idnumb=String.valueOf(resultset.getInt(1));
hidden_names = new Vector();
hidden_names.addElement(idnumb); //Id
// Fazer aqui um select da tabela ite, de cada produto de acordo com a id

consulta = "SELECT DB2ADMIN.ITEM.ITEM_IMAGEMREDUZIDA,DB2ADMIN.ITEM.IT EM_IMAGEMNORMAL FROM DB2ADMIN.ITEM WHERE DB2ADMIN.ITEM.ITEM_PRODUTOID = "+idnumb;
pstmt = connect.prepareStatement(consulta);
resultset2 = pstmt.executeQuery();

if(resultset2.next())
{
hidden_names.addElement(resultset2.getString(1));// imagem reduzida
hidden_names.addElement(resultset2.getString(2));// imagem normal
}

hidden_names.addElement(resultset.getString(2));// produtos descricao
hidden_names.addElement(resultset.getString(3));// produtos valor unitario
hidden_names.addElement(resultset.getString(4));// produtos iponivel
hidden_names.addElement(resultset.getString(5));// nome
hidden_names.addElement(resultset.getString(6));// Disponivel
hidden_names.addElement(String.valueOf(resultset.g etInt(7)));// TimeId

hidden_names.addElement(String.valueOf(resultset.g etInt(8)));// SelecaoId

hidden_names.addElement(String.valueOf(resultset.g etInt(9)));// ClubeId

Ids.addElement(String.valueOf(resultset.getInt(1)) ); // coloca os indices da selecao no vetor
produtos.put(String.valueOf(resultset.getInt(1)),h idden_names); // coloca na hashtable



contaregistros=contaregistros+1;
}
while(resultset.next());
}

}catch(Exception e){
e.printStackTrace();
}
finally{
resultset2.close();
resultset.close();
pstmt.close();
}



tot=(contaregistros/9);///// Acha o numero de paginas , caso seja feita uma selecao de de categoria
if((contaregistros%9)<=0)
{
totpaginas=Math.round(tot);
}
else
{
totpaginas=Math.round(tot)+1;
}

///// Fim de achar o numero de paginas , caso seja feita uma selecao de de categoria
%>
<%



int i=0;
int indiceachado=0;
String Idachado=null;
Hashtable produtostemp = new Hashtable();
Randomico produtorandomico ;
///////////////////////// faz a selecao se vai apanhar um conhunto de dados randomicamente(sem uma categoria ter sido escolhida
// ou nao

if (pagina==null)
{

if (contaregistros>0)
{
produtorandomico = new Randomico();
produtorandomico.setNumero(produtos.size());
while (produtostemp.size() <= 9)
{

//produtorandomico.setNumero(Ids.size()); // seta o limite de randomizacao pelo numero de registros retornados
indiceachado=produtorandomico.getNumero(); // pega um indice quaLQUER

Idachado= String.valueOf(Ids.elementAt(indiceachado)); // recupera o valor de id do produto

if ((produtostemp.containsKey(Idachado))== false)
{
produtostemp.put(Idachado,produtos.get(Idachado));//indice , vetor
i++;

}

if (i==contaregistros)
break;
}//While
}//IF

} // fim if pagina == null
else // se pagina # null
{
produtostemp = produtos;
}

///////////////////////// Fim da selecao se vai apanhar um conhunto de dados randomicamente(sem uma categoria ter sido escolhida
// ou nao



/////////////////////////// carregamento das Imagens ////////////


String img1desc = null;
String img1caminho = null;
String img1preco = null;
String img1nome = null;
String img1caminhonorm = null;
String img1id = null;
String img2desc = null;
String img2caminho = null;
String img2preco = null;
String img2nome = null;
String img2caminhonorm = null;
String img2id = null;
String img3desc = null;
String img3caminho = null;
String img3preco = null;
String img3nome = null;
String img3caminhonorm = null;
String img3id = null;
String img4desc = null;
String img4caminho = null;
String img4preco = null;
String img4nome = null;
String img4caminhonorm = null;
String img4id = null;
String img5desc = null;
String img5caminho = null;
String img5preco = null;
String img5nome = null;
String img5caminhonorm = null;
String img5id = null;
String img6desc = null;
String img6caminho = null;
String img6preco = null;
String img6nome = null;
String img6caminhonorm = null;
String img6id = null;
String img7desc = null;
String img7caminho = null;
String img7preco = null;
String img7nome = null;
String img7caminhonorm = null;
String img7id = null;
String img8desc = null;
String img8caminho = null;
String img8preco = null;
String img8nome = null;
String img8caminhonorm = null;
String img8id = null;
String img9desc = null;
String img9caminho = null;
String img9preco = null;
String img9nome = null;
String img9caminhonorm = null;
String img9id = null;
boolean visivel1=false;
boolean visivel2=false;
boolean visivel3=false;
boolean visivel4=false;
boolean visivel5=false;
boolean visivel6=false;
boolean visivel7=false;
boolean visivel8=false;
boolean visivel9=false;
String disponivel1=null;
String disponivel2=null;
String disponivel3=null;
String disponivel4=null;
String disponivel5=null;
String disponivel6=null;
String disponivel7=null;
String disponivel8=null;
String disponivel9=null;
String timeId1=null;
String timeId2=null;
String timeId3=null;
String timeId4=null;
String timeId5=null;
String timeId6=null;
String timeId7=null;
String timeId8=null;
String timeId9=null;
//
String selecaoid1=null;
String selecaoid2=null;
String selecaoid3=null;
String selecaoid4=null;
String selecaoid5=null;
String selecaoid6=null;
String selecaoid7=null;
String selecaoid8=null;
String selecaoid9=null;
//
String clubeid1=null;
String clubeid2=null;
String clubeid3=null;
String clubeid4=null;
String clubeid5=null;
String clubeid6=null;
String clubeid7=null;
String clubeid8=null;
String clubeid9=null;
Reply With Quote
  #13 (permalink)  
Old 09-06-06, 12:41
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Why not use "FETCH FIRST 49 ROWS ONLY" instead of using ROW_NUMBER() ?
That will also eliminate a subquery.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On