performance is slow while running a DB query using Struts - sql

I am using Struts to build a web application. Application server is Tomcat Server 6, database is Oracle 11G. I am executing an action class which contains the following DB query:
SELECT BV_USER__TEMP.CEC_ID AS USER_ID,
BV_USER_BEHRCHY_TEMP.LEVEL_NUM AS LEVEL_NUM,
BV_IAM_BUSINESS_ENTITY_HIER.BUSINESS_ENTITY_HIER_NAME AS IAM__HIER_NAME,
'Total Development' AS TOTAL_ENGINEERING,
BV_IAM_BUSINESS_ENTITY_HIER.BUSINESS_HIER_NAME AS keyCol,
BV_USER_BE_HRCHY_TEMP.BUSINESS_ENTITY_DESCR AS val
FROM BV_IAM_BUSINESS_ENTITY_HIER IAM
INNER JOIN BV_SUB_BUSINESS_ENTITY BSE
ON (BV_USER_BE_HRCHY_TEMP.SUB_BUSINESS_ENTITY_DESCR =
BV_IAM_BUSINESS_ENTITY_HIER.BUSINESS_ENTITY_HIER_NAME)
WHERE IAM_ROLE_NAME='FIN BI_USER LEVEL ACCESS'
AND IAM_LEVEL_NUM=2
AND USER_ID=?
GROUP BY 1,2,3,4,5,6
UNION ALL
SELECT CEC_ID AS USER_ID,
BUSINESS_ENTITY_HIER_NAME.IAM_LEVEL_NUM AS LEVEL_NUM,
BUSINESS_ENTITY_HIER_NAME AS keyCol,
BV_USER_BE_HRCHY_TEMP.BUSINESS_ENTITY_DESCR AS val
FROM BV_IAM_BUSINESS_ENTITY_HIER IAM
INNER JOIN BV_SUB_BUSINESS_ENTITY BSE
ON (BUSINESS_ENTITY_NAME=BUSINESS_ENTITY_HIER_NAME)
WHERE BV_IAM_BUSINESS_ENTITY_HIER.IAM_ROLE_NAME='FIN BI_USER LEVEL ACCESS'
AND BV_IAM_BUSINESS_ENTITY_HIER.IAM_LEVEL_NUM=1
AND USER_id=2
GROUP BY 1,2,3,4,5,6
UNION ALL
SELECT BV_USER_BE_HRCHY_TEMP.USER_ID,
BV_IAM_BUSINESS_ENTITY_HIER.IAM_LEVEL_NUM AS LEVEL_NUM,
BV_IAM_BUSINESS_ENTITY_HIER.BUSINESS_ENTITY_HIER_NAME AS IAM_ENTITY_HIER_NAME,
'Total Development' AS TOTAL_ENGINEERING,
BUSINESS_ENTITY_HIER_NAME AS keyCol,
BV_USER_BE_HRCHY_TEMP.BUSINESS_ENTITY_DESCR AS val
FROM BV_IAM_BUSINESS_ENTITY_HIER IAM
CROSS JOIN BV_USER_BE_HRCHY_TEMP.SUB_BUSINESS_ENTITY_DESCR BSE
WHERE BV_IAM_BUSINESS_ENTITY_HIER.IAM_ROLE_NAME='FIN BI_USER LEVEL ACCESS'
AND BV_IAM_BUSINESS_ENTITY_HIER.IAM_LEVEL_NUM=0
AND USER_ID=?
GROUP BY 1,2,3,4,5,6;
In applicationContext.xml the bean:
<bean id="tablOrclDataSourceRead" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:oci:#********DEV" />
<property name="username" value="username" />
<property name="password" value="password" />
<property name="initialSize" value="5" />
<property name="maxActive" value="5" />
<property name="maxIdle" value="0" />
<property name="minIdle" value="0" />
<property name="maxWait" value="-1" />
</bean>
JSON returned:
{"data":[
{"keyCol":"Utah","val":"Utah"},
{"keyCol":"Seattle","val":"Seattle"},
{"keyCol":Nebraska","val":"Nebraska"},
{"keyCol":"Enterprise Networking","val":"Enterprise Networking"}
]}
The time taken to execute the action (and return the JSON) with the above query is around 21 seconds.
Please let me know if I am missing something.
The same query when executed through SQL developer returns the result set in less than a second.

Related

Sql server xpath conditionnal query

I have a table where one field called 'configuration' is type of XML:
<configuration>
<element value="john" />
<element value="kevin" />
<element value="lisa" />
<element value="david" />
<element value="mike" />
</configuration>
What I would like to do, is to retrieve all the table records or at least count the table records that have a field 'configuration' containing at least one 'element' attribute containing a 'value' attribute equals to 'lisa'.
What I have for the moment, is a query that can retrieve the 'value' attribute of the specified 'element' position, for example:
select Configuration.value('(/configuration/element/#value)[0]', 'nvarchar(max)') // returns me 'john'
select Configuration.value('(/configuration/element/#value)[1]', 'nvarchar(max)') // returns me 'kevin'
This is a bit of pseudo-SQL in the absence of a dataset, and a bit of guesswork; in that I (think) you simply want to return the rows(?) where there is a the node configuration/element has the value property 'lisa'. IF my guess is right, then something like this will work (you'll ened to replace object names in Braces({}):
SELECT {Columns}
FROM [{Your Table}] YT
WHERE EXISTS (SELECT 1
FROM [{Your Table}] E
CROSS APPLY E.[{Your XML Column}].nodes('configuration/element') C(E)
WHERE E.[{Your ID Column}] = YT.[{Your ID Column}]
AND C.E.value('./#value','varchar(50)') = 'lisa');
Example:
WITH VTE AS(
SELECT 1 AS ID,
CONVERT(xml,'<configuration>
<element value="john" />
<element value="kevin" />
<element value="lisa" />
<element value="david" />
<element value="mike" />
</configuration>') AS XMlCol
UNION ALL
SELECT 2 AS ID,
CONVERT(xml,'<configuration>
<element value="craig" />
<element value="donald" />
<element value="jenny" />
<element value="jayne" />
</configuration>') AS XMlCol)
SELECT *
FROM VTE YT
WHERE EXISTS (SELECT 1
FROM VTE E
CROSS APPLY E.XMlCol.nodes('configuration/element') C(E)
WHERE E.ID = YT.ID
AND C.E.value('./#value','varchar(50)') = 'lisa');
This only returns the row with an ID of 1.
Please try the below,
declare #xml as xml
set #xml='<configuration>
<element value="john" />
<element value="kevin" />
<element value="lisa" />
<element value="david" />
<element value="mike" />
</configuration>'
SELECT T.c.value('./#value','nvarchar(250)' ) As element
FROM #xml.nodes('//element') AS T(c)

Extracting XML sub-tags from a clob in Oracle via SQL

I have the following CLOB XML Fragment in the DB attribute "object_body":
<object stamp="8fc630bd-f1f5-43ac-b9d2-3b9db9054c75_1481811010527" type="Aggregation" version="12">
<property name="name" value="COA_Cash_Position" valueType="string"/>
<property name="description" value="COA_Cash_Position" valueType="string"/>
<property name="objectId" value="ef4296f1-6af6-4de8-83fe-fa01cf327d87" valueType="string"/>
<property name="branchId" value="01ecc9ed-27f3-42cb-a44d-2cec221ec8fa" valueType="string"/>
<property name="models" valueType="table">
<object type="ModelBasedTask:modelEntry" version="3">
<property name="dataModel" valueType="url">DataModel["ModifyModel[COA_Cash_Positions_BHS_Unadjusted].STAGING:X_NRS_BSL40K_BHS"{ModifyModel[ff81e5f2-2ef6-437a-b113-67f7c0dd5e53].cdf308fc-520d-4611-a8db-4e6863e216b8:9ffcd08e-732a-4270-b15f-f4775bcc69db}]</property>
<property name="instanceSelectionRule" valueType="object">
<object type="InstanceSelectionRule" version="3">
<property name="instanceDateRule" valueType="object">
<object type="InstanceSelectionRule:rule" version="3">
<property name="type" value="EQUAL" valueType="string"/>
</object>
</property>
<property name="instanceKeyRules" valueType="table"/>
</object>
</property>
</object>
</property>
</object>
Aim is to extract the value of the property tag with name=”description”
=> to produce “COA_Cash_Position”
Tried all variations of:
EXTRACTVALUE(xmltype(object.object_body), '/object/property[#name="description"].getStringVal()'),
If I loose the .getStringVal() the query works but obviously only provides empty results (as the property tag itself doesn't include any value).
Get mostly error messages the likes of:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00601: Invalid token in: '/object/property[#name="description"].
getStringVal()'
What easily works is extracting the values between two xml tags but getting a specific property value (like here the "value" of the tag with name="description") seems to evade the XML extract possibilities.
Any help is warmly appreciated...
EXTRACTVALUE is deprecated in Oracle 12 - it is being replaced by XMLTABLE or XMLQUERY.
SELECT x.description
FROM your_table t
CROSS JOIN
XMLTABLE(
'//object/'
PASSING XMLTYPE( t.your_clob_column )
COLUMNS description VARCHAR2(4000) PATH './property[#name="description"]/#value'
) x;
or
SELECT XMLQUERY(
'//object/property[#name="description"]/#value'
PASSING XMLTYPE( your_clob_column )
RETURNING CONTENT
).getStringVal()
FROM your_table;
But if you do want to use EXTRACTVALUE you can do:
SELECT EXTRACTVALUE(
XMLTYPE( your_clob_column ),
'//object/property[#name="description"]/#value'
)
FROM your_table;
Assuming you mean the value of the attribute #value the path you want is
/object/property[#name='description']/#value
Your XML contains some errors.
We can achieve your expected output using REGEXP in oracle.
Check below query.
SELECT REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_SUBSTR (object_body, 'description" value=".*.".v', 1), 'description" value="', '', 1, 1) ,'" v','',1,1)
FROM Table
Check Demo here.
Output IS: COA_Cash_Position

Update Single XML Node Value of XML Column using SQL Server

I want to update single value of XML Node in SQl Server
Below is the table structure
XML Structure
<PayDetails>
<Column Name="FG" DataType="float" Value="7241" />
<Column Name="SKILL" DataType="float" Value="3" />
<Column Name="PI" DataType="float" Value="87" />
<Column Name="MD" DataType="float" Value="30" />
<Column Name="LD" DataType="float" Value="4" />
<Column Name="WEEKOFF_DAYS" DataType="float" Value="4" />
<Column Name="NETPAY" DataType="float" Value="5389" />
</PayDetails>
I want to update value of FG from 7241 to 8000
You want to use replace value of...with keywords:
Try something like the following:
update tablename
set TransactionFieldDetails.modify(
'replace value of
(/PayDetails/Column[#Name="FG"]/#Value)[1]
with "8000"');

JdbcPagingItemReader with multiple select query

I 'd like to write a JdbcPagingItemReader with a complex query like:
select xxx from yyy where a=b and c = (select max(f) from yyy where a=b)
...with parameters.
Here is how I wrote my query:
<bean
class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="selectClause" value="SELECT ohrefnum" />
<property name="fromClause" value="FROM orderhdr_all" />
<property name="whereClause"
value="WHERE customer_id =: CUSTOMER_ID AND ohentdate = (SELECT MAX (ohentdate) FROM orderhdr_all WHERE customer_id =: CUSTOMER_ID)" />
<property name="sortKey" value="ohrefnum" />
</bean>
but it raise the following error: java.sql.SQLException: Missing IN or OUT parameter at index:: 2
I found the problem origin. It was simply the space character after the "=:"
It should be
WHERE customer_id =:CUSTOMER_ID)
and not
WHERE customer_id =: CUSTOMER_ID)

Add text to the start of a SOLR database value

I am using this code for my SOLR DIH:
<dataConfig>
<dataSource name="app" driver="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/wikipedia" user="wikipedia" password="secret" />
<dataSource name="data" driver="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/wikipedia" user="wikipedia" password="secret" />
<document>
<entity dataSource="app-ds" name="item" query="SELECT id, title, description, date, link, location_id, source_id, company_id from item">
<field column="id" name="id" />
<field column="title" name="title" />
<field column="description" name="description" />
<field column="date" name="date" />
<field column="link" name="link" />
<entity dataSource="app-ds" name="location" query="SELECT name, coordinate from location where location_id=${item.location_id}">
<field column="name" name="location_name" />
<field column="coordinate" name="location_coordinates" />
</entity>
<entity dataSource="app-ds" name="source" query="SELECT name from source where source_id=${item.source_id}">
<field column="name" name="source_name" />
</entity>
<entity dataSource="app-ds" name="company" query="SELECT name from company where company_id=${item.company_id}">
<field column="name" name="company_name" />
</entity>
</entity>
</document>
</dataConfig>
Since I am merging two databases I want to have a uniqueID for each entry within SOLR. In my case the best way of doing this is to have app*ID* for the first databases ID's and data*ID* for the second databases ID's.
Using my code above, how do I add the word "app" to the front of the ID that is stored in the SOLR ID field so that my database ID=123 and the Solr ID = app123
EDIT: As I guess it might be something like this (but I am not good with SQL)
query="SELECT app_(id)
You can try to modify the SQL Query as -
SELECT 'TABLE1' || ID AS PRIMARY_ID ........
PRIMARY_ID can be now configured as unique ID in solr.
<field column="primary_id" name="primary_id" />
Even if you are performing incremental updates using Delta imports, the SQL ID updated would still generate the Same Solr ID with the above query and would be updated. So be sure to use the above in the Delta queries as well.