Hello, I have not found a lot of information in searches I've done on ... but I have a problem currently that is as follows:
I have a query with many simple conditions, just sequences of and . But today I needed to include some or , and behold the query simply can not finish, it runs for minutes until it stops because it exceeds the PostgresSQL memory limit or something. So I thought about separating those or within a block, in which case I put them in parentheses and then the query ran very quickly and brought me a result without errors. So I decided to do a test with another query, this time was much simpler and ... when I put the parentheses, the query now did not bring me anything else, but if I remove them, it executed as expected.
So I started to wonder, what was really going on and what is the correct procedure to do and how to use the parentheses in WHERE. Thank you in advance!
Example of the query I'm using without parentheses:
nfe.id_empresa = 4 and
nfe.ano = '2016' and
nfe.id_empresa = nfe_item.id_empresa and
nfe.ano = nfe_item.ano and
nfe.mes = nfe_item.mes and
nfe_item.cst_icms = '010' or
nfe_item.cst_icms = '030' or
nfe_item.cst_icms = '060' or
nfe_item.cst_icms = '070' or
nfe_item.cst_icms = '110' ;
Now with parentheses:
nfe.id_empresa = 4 and
nfe.ano = '2016' and
nfe.id_empresa = nfe_item.id_empresa and
nfe.ano = nfe_item.ano and
nfe.mes = nfe_item.mes and
(
nfe_item.cst_icms = '010' or
nfe_item.cst_icms = '030' or
nfe_item.cst_icms = '060' or
nfe_item.cst_icms = '070' or
nfe_item.cst_icms = '110'
);