Query tuning in WHERE clause
Hi All,
I have 2 tables.
TABLE_A contains 10 miliion records.
TABLE_B contains 100 Records.
I am joining these 2 tables
Here a.ID is in Left side
SELECT A.*, B.*
FROM TABLE_A a, TABLE_B b
WHERE a.ID = b.ID;Here b.ID is in Left side
SELECT A.*, B.*
FROM TABLE_A a, TABLE_B b
WHERE b.ID = a.ID;My question is which query will give better performance? I was thinking, that, if we put table column with less number of rows at Left side, then it will be faster than other.
In current versions of Oracle, it doesn't matter in what order you sum-up your predicates in the WHERE-clause of a query: they will be optimized/executed in a same way.
Similar Messages
-
Query in the where clause is not allowed
Hi all,
I have a table m_vehicle that stores 1 or more than 1 rows of data for each person. But the partner's schema requires that 0 or 1 vehicle license plate data be sent with the xml file. I have the following nested select statement inside a bigger one and it does not work,
select xmlelement("VehicleLicensePlate",
(case when mv.lic_plate_state is not null then
xmlelement("PlateState", mv.lic_plate_state)
end), -- end of plate state
(case when mv.lic_plate_no is not null then
xmlelement("PlateNumber", xmlelement("ID", mv.lic_plate_no))
end),
(case when mv.lic_plate_year is not null then
xmlelement("PlateExpiryYear", mv.lic_plate_year)
end)
).extract('/*')
from m_vehicle mv where mv.mni_id = 1083650
and mv.seq_no = (select max(mv2.seq_no) from m_vehicle mv2
where mv2.mni_id = mv.mni_id) and mv.lic_plate_no is not null
But it works when I run it alone and gets the latest row for the person. It does not work either if I join this table with other tables. My root element select is from dual because I have to supply lots of SOAP and other artifacts in the xml. Is it because I have this root select from dual that the query in the where clause does not work? How do I make it work so that I get 0 or one row only from the table? My solution was to write a function that returns each part of the license plate data using dymanic sql and call it in the xmlelement function. It works but I would like to use plain sql instead.
Thanks.
BenBy the way, the error I get is this,
ERROR:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00216: invalid character 3 (0x3)
Error at line 1
ORA-06512: at "SYS.XMLTYPE", line 111
Ben -
Very slow query when specifying where clause
Hi folks:
I am using SAS with SQL Passthrough for Oracle, however that may be irrelevant to the problem.
I have a query that I am subsetting based on a series of where clauses, and when I put the clauses into the query, it slows it down tremendously.
I can pull the entire table and subset it later on in SAS and it is much faster.
Can someone explain why the SQL subsetting logic runs slower than pulling 18M rows and subsetting later?
Here is my query - hope this is enough information
select
a.loan_number
, a.CFLRC_CD
, a.cservno
, b.cprj_typ
, a.occ_stat
, c.state2
, c.city2
, e.hstatus
, e.hcattype
, e.dqind
, e.mthsdel
from
VEW_LL_ln_1 a
, vew_ll_ln_2 b
, VEW_LL_PROP_GEO_CD c
, vew_ll_ln_actvy_1 e
where
a.loan_number = b.loan_number
and a.loan_number = c.loan_number
and a.loan_number = e.loan_number
and e.act_dte = '01-feb-2011'
and not (occ_stat in ('2','3'))
and hcattype <> '800'
and dqind not in ('E','F','G','H')
and cprj_typ not in ('1','2','3','4','5','16','17','18','19','20','21','22')
and cflrc_cd <> '2'
order by
a.loan_number
ThanksYou're in the wrong forum. This is the forum for the SQL Developer tool. You will get better answers in the SQL and PL/SQL forum which is here PL/SQL
-
Data Services Designer Query tranform dynamic where clause or another way
I have an XML source that has 5 fields. These fields are used to query 4 different SQL table data sources and do an AND inthe where clause of a query transform. Works well if all 5 fields in the xml have data. If one (or more) are blank, then it runs the where checking for blank matches and gives no results. What I want to do is if a field is blank, do not use it in the where clause. Could this be an outer join setup? Issue is that if there is a PhoneNumber input, then to be considered a result, there must be a PhoneNumber match on potential result records...
I have an XML source that has 5 fields. These fields are used to query 4 different SQL table data sources and do an AND inthe where clause of a query transform. Works well if all 5 fields in the xml have data. If one (or more) are blank, then it runs the where checking for blank matches and gives no results. What I want to do is if a field is blank, do not use it in the where clause. Could this be an outer join setup? Issue is that if there is a PhoneNumber input, then to be considered a result, there must be a PhoneNumber match on potential result records...
-
Merge query error in Where clause
Following error is coming when i execute the merge query. Anything wrong with this? I am using Oracle 9.2.0.1.
Query:
MERGE
INTO incompletekalls ic
USING live_small ls
ON ((ls.callid = ic.callid) AND
(ls.sdate = ic.sdate) AND
(ls.stime = ic.stime))
WHEN MATCHED THEN
UPDATE
SET ic.adate = ls.adate,
ic.atime = ls.atime,
ic.edate = ls.edate,
ic.etime = ls.etime
WHERE
ls.sdate = '16-Apr-09' AND ls.stime >= '09:00:00' AND ls.stime <= '11:00:00' AND ((ls.adate IS NULL) OR
(ls.edate IS NULL))
WHEN NOT MATCHED THEN
INSERT (ic.callid,ic.cg,ic.cd,ic.re,ic.opc,ic.dpc,ic.sdate,ic.stime,ic.adate,ic.atime,ic.edate,ic.etime)
VALUES (ls.callid,ls.cg,ls.cd,ls.re,ls.opc,ls.dpc,ls.sdate,ls.stime,ls.adate,ls.atime,ls.edate,ls.etime)
WHERE ls.sdate >= '16-Apr-09' AND ls.stime >= '09:00:00' AND ls.stime <= '11:00:00'
Error:
SQL> /
WHERE
ERROR at line 13:
ORA-00905: missing keywordHi,
From looking at the documented examples
http://www.oracle.com/pls/db92/db92.drilldown?levelnum=2&toplevel=a96540&method=FULL&chapters=0&book=&wildcards=1&preference=&expand_all=&verb=&word=MERGE#a96540
and on http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:5318183934935
I think that you cannot use the WHERE in your MERGE like that on 9i...
Something else I want to warn you for:
It's a bad idea to store your date and time separated as strings! You'll run into troubles sooner or later, for 100%...
Use a single DATE column instead, in which you store both the date and time components.. -
Query...where clause problem in XSQL
I am getting error with following simple example: (however it works fine when the < sign changed to greater than > sign). Anybody any idea?
select * from emp
where empno < 7876
XSQL-005: XSQL page is not well-formed.
XML parse error at line , char
Expected name instead of .
nullThe "less-than" sign (also known as the "left-angle-bracket") is a special character in an XML file, since it is the start of an element like <foo>.
So, you either have to write your query like this:
<xsql:query>
select * from emp where empno &;lt; 7839
</xsql:query>or like this, escaping the entire query with a CDATA section:
<xsql:query>
<![CDATA[
select * from emp where empno < 7839
]]>
</xsql:query>A third option is that instead of writing:
empno < 7839
you exploit the fact that this is equivalent to:
7839 > empno
and write it that way. Since "greater-than" sign does not need to be escaped specially in an XML file, it gets the job done, too. -
DocumentClient query with Int64 where clause is incorrect when converted and sent to Azure
The code below constructs a query with a lambda expression involving an Int64. When the query is converted and sent to Azure, it is incorrect. I captured the request to Azure using Fiddler for the example below, and the body contained:
{"query":"SELECT * FROM root WHERE (root.car.id = 1.01036229983142E+16) "}
Notice the constant does not match that of the code. Is this a bug in Azure DocumentDB library? If so, where can I submit a bug?
long carId = 10103622998314238;
IQueryable<MyDocument> query = documentClient.CreateDocumentQuery<MyDocument>("<collectionLinkGoesHere>", new FeedOptions { MaxItemCount = 1 });
query = query.Where((MyDocument d) => d.Car.Id == carId);
await query.AsDocumentQuery().ExecuteNextAsync<T>();@kurifodo1, DocumentDB follows the JSON standard for numbers, which is http://en.wikipedia.org/wiki/IEEE_754 notation. So any long integer gets serialized to a double stored as IEE754, and will lose some precision. While this isn't the most intuitive behavior,
it's important in order to be compliant with other tools and platforms that speak JSON.
If you'd like to just perform equality queries, consider storing this as a string. If you'd like to perform range queries, consider breaking up the number into two parts (high and low), similar to the BigInteger structure in C#.
Hope this helps. -
Set_block_property does not called the query from the where clause
Hi all,
I have two blocks in the Oracle form 6i. Master link with the detail. Because of the data is quite huge if I have the auto query when the form is loaded hence I have two enter parameter created of From_date and To_date together with button to execute the query when that two parameter is being filled.
I have the following quotes in the button of when-button-pressed :-
BEGIN
set_block_property('act_vw_sale_comm', default_where, 'invh_dt >='''|| to_date(:act_vw_sale_comm.fdate,'dd/mm/yyyy') ||''' and invh_dt <= ''' || to_date(:act_vw_sale_comm.tdate,'dd/mm/yyyy')||'');
go_block('act_vw_sale_comm');
execute_query;
set_block_property('act_vw_sale_comm', default_where, '');
END;
However, nothing being displayed when I click on the button. Anything wrong on the above statement ? Please help me. thanks.
Rgds
LimBEGIN
go_block('act_vw_sale_comm');
set_block_property('act_vw_sale_comm', default_where, 'invh_dt >= '''|| :FDATE ||''''' AND invh_dt <= '''|| :TDATE ||'''');
execute_query;
--set_block_property('act_vw_sale_comm', default_where, '');
END;
Now, I have the above statement, but still the same when try to execute the query, this time, the screen execute but does not show anything and close my form straight away. Why!
Anyone can help me?
Thanks
Lim -
Tuning a where clause with between
Hi,
I have a 3 million row table which I'm accessing in the following manner;
select *
from tabA
where :1 between col1_from and col1_to
and col2 = :2
I have an index on (col1_from, col1_to), it's takes approx 0.8 seconds to execute. I've been asked to tune this further (down to 0.1 preferably).
I've tried a few things including bitmap, swaping indexed columns around, but I can't get it below 0.8 seconds. It seems the BETWEEN is the culprit as 'select..where :1 = col1_from' whizzes through in no time.
Both from/to columns are varchar2.
Any ideas please?Just a couple of things to add here.
Not sure what version you are on, but in earlier versions of Oracle the CBO had problems with range based operations (guessing the cardinality wrong) so simply reordering the predicate could be useful to you.
select *
from tabA
where col2 = :2
and :1 between col1_from and col1_to
I've tried a few things including bitmapJust want to make sure you know that if this table is a transactional table you don't want to be adding bitmap indexes to it. In your test environment where there are no modifications to this table it might run well and good, and you may meet your 0.1 second SLA, but you'll be in a rough state when this gets pushed into production. -
Query Execution on Where Clause
Hi I have the where cluase as below , I would like to know how does oracle decides which one to execute first,
WHERE S.PERSPECTIVE='S'
and s.shipment_gid=sb.shipment_gid
AND SB.BILL_GID=CBIL.INVOICE_GID
and inv.invoice_gid=cbil.invoice_gid
AND S.SOURCE_LOCATION_GID=LC.LOCATION_GID
and l.location_gid=lc.location_gid
AND TRUNC(cbil.insert_date)=TRUNC(tc.tesco_cal_date)
AND(lc.location_gid='N' OR lc.corporation_gid='TESCO.10719')
AND s.source_location_gid=lc.location_gid
AND tc.tesco_year='2013'
AND tc.tesco_period=6
AND tc.tesco_week_number=23Hi,
The optimizer usually goes for low-hanging fruit first. If there is a condition that's easy to perform and narrows down the result set a lot, it will do that condition before another one that is harder to perform, or that doesn't rule out so many rows. For example, if there are indexes on tc.tesco_period and tc.tesco_week_number, but not on tc.tesco_year, then it will probably perform one of the conditions that can be done with an index before it does the condition involving tc.tesco_year. If there are 53 different possible values for tc.tesco_week_number, but only 6 possible values for tc_tesco_period, then it will probably to the condition on tc.tesco_week_number (and rule out about 52/53 of the rows) before it does the condition thatt involves tc.tesco_period (which only excludes about 5/6 of the rows.)
There are always exceptions. As others have said already, look at the execution plan if you really want to know. -
How do you use 3 Where Clauses in a query
Hi, i am trying to figure out how to use 3 Where Clauses in a Query where 2 of the Where Clauses uses a Sub query.
Display the OrderID of all orders that where placed after all orders placed by “Bottom-Dollar Markets”.
Order the result by OrderID in ascending order.
First WHERE clause checks for OrderDate and uses a sub query with ALL keyword.
Second WHERE clause use equals and sub query.
Third WHERE clause uses equal and company name.
This is what i have so far but i am pretty confused on how to do this.
My Code for NorthWind:
Select OrderID
From Orders o
Where o.OrderID IN (Select OrderDate From Orders Where Orders.OrderID > ALL
(Select CompanyName From Customers Where CompanyName = 'Bottom-Dollar Markets'));
The book shows how to use the ALL Keyword but not in a Sub query with Multiple Where Clauses.
Select VenderName, InvoiceNumber, InvoiceTotal
FROM Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE InvoiceTotal > ALL (Select InvoiceTotal From Invoices Where VendorID = 34)
ORDER BY VendorName;>Where Orders.OrderDate
> ALL (Select
CompanyName
The comparison operator (>) requires compatible data types.
DATETIME is not compatible with VARCHAR string for comparison.
Here is your homework:
SELECT orderid
FROM orders o
WHERE o.orderdate > ALL (SELECT orderdate
FROM orders
WHERE shipvia = (SELECT Max(shipvia)
FROM orders o
INNER JOIN customers c
ON c.customerid =
o.customerid
WHERE
c.companyname = 'Bottom-Dollar Markets'));
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
Kalman Toth Database & OLAP Architect
SQL Server 2014 Database Design
New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014 -
Dynamic query in where clause while looping in an internal table.
Hi,
Had a small question : How can i make a dynamic query for the WHERE clause while looping at an internal table.
i want to implement a dynamic where clause query for the below example.
it_cfx_col is an internal table and wa_cfx_col is a work area for it_cfx_col
DATA :
i_cfx_col TYPE TABLE OF cfx_col,
wa_cfx_col LIKE LINE OF i_cfx_col.
DATA : count TYPE i VALUE 0.
DATA : l_where_clause TYPE string,
l_where_clause2 TYPE string,
l_name type string.
l_name = 'NANDANOM'.
l_scenario = 'collaboration'.
LOOP AT it_cfx_col INTO wa_cfx_col
WHERE CREATED_BY = l_name
AND SCENARIO = l_scenario.
count = count + 1.
some business logic implemented using the work area wa_cfx_col
endloop.
Now i want to write a dynamic query for the where clause.
DATA : count TYPE i VALUE 0.
DATA : l_where_clause TYPE string,
l_where_clause2 TYPE string,
l_name type string.
l_name = 'NANDANOM'.
l_scenario = 'collaboration'.
l_where_clause = 'CREATED_BY = l_name'.
l_where_clause2 = 'AND SCENARIO = l_scenario'.
if l_scenario is not initial.
CONCATENATE l_where_clause l_where_clause2
INTO l_where_clause SEPARATED BY space.
endif.
LOOP AT i_cfx_col INTO wa_cfx_col
WHERE (l_where_clause).
count = count + 1.
some business logic implemented using the work area wa_cfx_col
endloop.
when i compile this i get an error message as { Statement concluding with "...(l_where_clause)" ended unexpectedly}
Even i changed the initilization of the variable l_where_clause2 to [ l_where_clause2 = 'AND SCENARIO = l_scenario.'. ]
added the end of line demarkation ".", but still i got the same error message.
Is it a limtation in ABAP that i cannot write a dynamic query for the where clause while looping at an internal table?
Regards,
omHi savita,
there in no such 1 limitaion in abap for dynamic query .. i think the error meassge is only beacuse of your synatx delcartaion.
>> LOOP AT i_cfx_col INTO wa_cfx_col
WHERE (l_where_clause).
count = count + 1.
some business logic implemented using the work area wa_cfx_col
endloop.
afted delclarataion also , in the where statement you should specify both the field name and value bname
LOOP AT i_cfx_col INTO wa_cfx_col
WHERE l_where_clause = 'CREATED_BY = l_name' .
count = count + 1.
hope it helps.
regads
priya. -
Issue in select query with where clause
Hi guys,
I'm facing an issue while using select query with the where clause. When I'm selecting all the data from the table it returns the correct result. But when I'm using the where clause to get the specific rows from the table it returns no rows. But the data I'm trying to fetch using the where condition exists in the table.
Here is my query which causing the issue,
select * from mytable where myfield = 'myvalue'
But if I use the following query it returns the result correctly.
select * from mytable
Also the myfield value 'myvalue' exists in the table.
I have tried by running this query in both SQL Developer and SQL Plus. I have tried this query in mssql as well. It works perfectly and returns correct result sets for both the queries I have mentioned above. I'm unable to predict the issue as I'm new to ORACLE. Please help.
Thanks,
Ram.Hi Ram,
I experienced an issue similar to this with a varchar2 field. Some of our records had a hidden newline character at the end of them, which was making queries like the one below fail:
select * from employees
where email = '[email protected]'The best way I found to detect this was to use
select 'XX'||email||'XX' from employeesTo make sure that there were no newlines. But that is just a guess. If you could provide some example table data and the outputs of your selects, it would be helpful.
Jeff -
The issue with using the multiple columns sub-query in WHERE clause
Hi All,
my database version is 10.2.
the problem i am trying to deal with is that when I use multiple column sub-query in the WHERE clause of the SELECT statement, the actual row number returned from the sub-query is different from the whole statement.
And what I found is that, whenever there is NULL in any of those columns returned from the SUB-QUERY, the outer query will just return NULL for that whole row.
Here is an example:
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t;
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT NULL 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
select * from EMP where (EMPNO, ENAME, job, MGR, HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 If I specifically deal with the NULL situations for the columns which might return NULL, I can get the result right.
select * from EMP where (EMPNO, ENAME, job, NVL(MGR,-1), HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from EMP
intersect
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT null 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 the problem is that, I feel this is a very lame way of handling it.
So, I wonder or am asking if there is any better or standard way to do it?
any help would be highly appreciated.
ThanksHi,
As you discovered, INTERSECT treats NULL as a value, but IN does not.
What you did with NVL is one way to handle the situation. If there was a chance that any of the columns could be NULL, then you might prefer something like this:
select *
from EMP
where ( EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
) in (
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from emp
intersect
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from t
);This assumes that you can identify some string (I used '~') that never occurs in the strings in these tables.
This is implicitly converting the NUMBERs. That's usually not a good thing to do. but explicitly converting them would make this even more tedious.
You should explicitly convert any DATEs to strings, however. Depending on your default format, and your data, you might get away with implicit conversions even for DATEs, but don't bet on it.
If you had to do this often, you might consider writing a user-defined function:
delimited_string (empno, ename, job, mgr, hiredate, sal, deptno) would return a string like
'7839~KING~PRESIDENT~~17-NOV-1981~5000~10'
This will make the coding easier, but beware: it will make the execution slower. -
Query SQL datastore with XML where clause source
Hope I am in the right place. New to Bus Obj Data Services Designer....I have cerated xml schemas, added it to the page as an xml source in. Mapped a test xml file and all is well there. I have added a query that grabs the xml.
I need to then query the MS SQL datastore ans use the data form the xml query as the where clause. How is this done? Or do I put a query on the datastore for all the data in a table then do anotehr query filtering one with the other? seems like that would be rather heavy and low performance. The results will then be sent back out as xml (schema and test file already set up as an xml out)
Thanks!Thanks for the tips.
I'm trying to implement this option, using your ViewDefHelper.
I´m running into a problem though. After I create my dynamic View Object using a ViewDef, I need to create some view links.
So I get the AttributeDefs of the columns (source, and destination) from the method findAttributeDef (which is the whole purpose of performance in my post). This method is returning the correct Attribute Def, but when I create the view Link with the method createViewLinkBetweenViewObjects(java.lang.String vlName,
java.lang.String accessorName,
ViewObject master,
AttributeDef[] srcAttrs,
ViewObject detail,
AttributeDef[] destAttrs,
java.lang.String assocClause)
My destination query is generating the where clause as:
null = ?
Any Ideas what I'm doing wrong ?
Thanks again.
John.
Maybe you are looking for
-
Hi Experts I configure smb.conf file and i ran test parm and started my smbd and nmb service also i am getting given below error message when i used to connect from my XP box to solaris 10 i checked the firewall it is disabled. Error: The network con
-
Cisco Agents goes to Not ready state
Hi, I have use IPCC ver:4.0(5) SR01ES09_Build095. We have encountered that our cisco agents automatically change state and it will goes from log-in state to Not ready state. what was the solution where i have to check the logs. regards Salman
-
Hi mate, Can you please let me know where the configurations are done for Gross/net goods receipt and invoice receipt ?
-
How to control a reference type of BAM Relation to see Related Activites on BAM Portal?
Hi. There is description of predefined reference types of BAM relations. I have created relation between two activities by adding Relationship node to one of them (to an activity that is tracking detailed orchestration). Both activity are declared
-
When I open itunes, my library is empty. But there is an icon on my desktop that is labeled "i-tunes music library". How do I get it back into my i-tunes