BigQuery - BATCH mode queries randomly failing with resources exceeded - google-cloud-platform

A part of our application submits 100s of BQ queries to build an aggregated dashboard view. Due to the large number of queries, we submit all queries with BATCH priority.
In last 24 hours we've had quite a few queries (more than 300) fail due to resources exceeded. This is odd as some of the failed queries save results to an output table specifically to avoid this type of issue. We spot checked a few others and are able to run them without issue in the BQ web UI.
Sample job ID (no dest table): job_GPghMyEBvlYFBGBHI5Szv0HMIpMY
Query:
SELECT order_sub.value as value, count(*) as count
FROM
(
SELECT order_id, drop_ship as value
FROM (TABLE_QUERY(d34f20170905T212849Zc4abca, 'table_id IN ("order_detail_2018")'))
WHERE order_date BETWEEN TIMESTAMP("2018-01-01") AND TIMESTAMP("2018-08-31")
) order_sub
JOIN EACH
(
SELECT customer_id, order_id, sequence
FROM temp_30_days.query_q6109c34f20180918T151206Ze52003_buyers
) cust_sub
ON order_sub.order_id = cust_sub.order_id
GROUP BY value
ORDER BY value
Error Message:
"status": {
"state": "DONE",
"errorResult": {
"reason": "resourcesExceeded",
"message": "Resources exceeded during query execution."
},
"errors": [
{
"reason": "resourcesExceeded",
"message": "Resources exceeded during query execution."
}
]
}
Sample job ID (with dest table): job_2YsCt-cxql5TP0jklb1aRENzINdZ
Query:
SELECT
customer_id, sequence
FROM
(
SELECT
customer_id,
sequence,
random,
MAX(random) OVER (PARTITION BY customer_id) AS max_value
FROM
(
SELECT customer_id, sequence, RAND() as random
FROM (TABLE_QUERY(d34f20170905T212849Zc4abca, 'table_id IN ("cust_char_2015", "cust_char_2016", "cust_char_2017")'))
WHERE end_date >= TIMESTAMP("2016-12-31")
AND effective_date <= TIMESTAMP ("2017-08-31")
AND (frequency_end = "98")
)
)
WHERE max_value = random
GROUP EACH BY
customer_id, sequence
Error Message:
"status": {
"errorResult": {
"message": "Resources exceeded during query execution.",
"reason": "resourcesExceeded"
},
"errors": [
{
"message": "Resources exceeded during query execution.",
"reason": "resourcesExceeded"
}
],
"state": "DONE"
}
FWIW status.cloud.google.com shows green lights for BigQuery, is anyone else experiencing similar issues?

Related

Oracle Apex - REST data source - nested JSON array - sync two tables - where to write SQL

This question is a follow up to another SO question.
Summary: I have an API returning a nested JSON array. Data is being extracted via APEX REST Data Sources. The Row Selector in the Data Profile is set to "." (to select the "root node").
The lines array has been manually added to a column (LINES) to the Data Profile, set data type to JSON Document, and used lines as the selector.
SAMPLE JSON RESPONSE FROM API
[ {
"order_number": "so1223",
"order_date": "2022-07-01",
"full_name": "Carny Coulter",
"email": "ccoulter2#ovh.net",
"credit_card": "3545556133694494",
"city": "Myhiya",
"state": "CA",
"zip_code": "12345",
"lines": [
{
"product": "Beans - Fava, Canned",
"quantity": 1,
"price": 1.99
},
{
"product": "Edible Flower - Mixed",
"quantity": 1,
"price": 1.50
}
]
},
{
"order_number": "so2244",
"order_date": "2022-12-28",
"full_name": "Liam Shawcross",
"email": "lshawcross5#exblog.jp",
"credit_card": "6331104669953298",
"city": "Humaitá",
"state": "NY",
"zip_code": "98670",
"lines": [
{
"order_id": 5,
"product": "Beans - Green",
"quantity": 2,
"price": 4.33
},
{
"order_id": 1,
"product": "Grapefruit - Pink",
"quantity": 5,
"price": 5.00
}
]
},
]
The order attributes have been synchronized to a local table (Table name: SOTEST_LOCAL)
The table has the correct data. As you can see below, the LINES column contains the JSON array.
I then created an ORDER_LINES child table to extract the JSON from LINES column in the SOTEST_LOCAL table. (Sorry for the table names.. I should've named the tables as ORDERS_LOCAL and ORDER_LINES_LOCAL)
CREATE TABLE "SOTEST_ORDER_LINES_LOCAL"
( "LINE_ID" NUMBER,
"ORDER_ID" NUMBER,
"LINE_NUMBER" NUMBER,
"PRODUCT" VARCHAR2(200) COLLATE "USING_NLS_COMP",
"QUANTITY" NUMBER,
"PRICE" NUMBER,
CONSTRAINT "SOTEST_ORDER_LINES_LOCAL_PK" PRIMARY KEY ("LINE_ID")
USING INDEX ENABLE
) DEFAULT COLLATION "USING_NLS_COMP"
/
ALTER TABLE "SOTEST_ORDER_LINES_LOCAL" ADD CONSTRAINT "SOTEST_ORDER_LINES_LOCAL_FK" FOREIGN KEY ("ORDER_ID")
REFERENCES "SOTEST_LOCAL" ("ORDER_ID") ON DELETE CASCADE ENABLE
/
QuickSQL version..
SOTEST_ORDER_LINES_LOCAL
LINE_ID /pk
ORDER_ID /fk SOTEST_LOCAL references ORDER_ID
LINE_NUMBER
PRODUCT
QUANTITY
PRICE
So per Carsten's answer in the previous question, I can write SQL to extract the JSON array from the LINES column in the SOTEST_LOCAL table to the child table SOTEST_ORDER_LINES_LOCAL.
My question is two parts.
Where exactly do I write the SQL? Would I write it in SQL Workshop in SQL Commands?
The REST data is being synchronized to make a request every hour. So would I need to write a function that runs every time there is new data being merged?
there are multiple options for this:
Create a trigger on the local synchronization table
You could create an trigger on your ORDERS table, which runs AFTER INSERT, UPDATE or DELETE on your ORDERS table, and which maintains the LINES table. The nice things about this one is that the maintenance of the child table is independent from APEX or the REST Synchronization; it would also work if you just inserted rows with plain SQL*Plus.
Here's some pseudo-code on how the trigger could look like.
create or replace trigger tr_maintain_lines
after insert or update or delete on ORDERS_LOCAL
for each row
begin
if inserting then
insert into SOTEST_ORDER_LINES_LOCAL ( order_id, line_id, line_number, product, quantity, price)
( select :new.id,
seq_lines.nextval,
j.line#,
j.product,
j.quantity,
j.price
from json_table(
:new.lines,
'$[*]' columns (
line# for ordinality,
product varchar2(255) path '$.product',
quantity number path '$.quantity',
price number path '$.price' ) ) );
elsif deleting then
delete SOTEST_ORDER_LINES_LOCAL
where order_id = :old.id;
elsif updating then
--
-- handle the update case here.
-- I would simply delete and re-insert LINES rows.
end if;
end;
Handle child table maintenance in APEX itself.
You could turn off the schedule of your REST Source synchronization, and have it only running when called with APEX_REST_SOURCE_SYNC.SYNCHRONIZE_DATA (https://docs.oracle.com/en/database/oracle/apex/22.1/aeapi/SYNCHRONIZE_DATA-Procedure.html#GUID-660DE4D1-4BAF-405A-A871-6B8C201969C9).
Then create an APEX Automation, which runs on your desired schedule, and this automation has two Actions. One would be the REST Source Synchronization, the other one would call PL/SQL code to maintain the child tables.
Have a look into this blog posting which talks a bit about more complex synchronization scenarios (although it does exactly fit scenario): https://blogs.oracle.com/apex/post/synchronize-parent-child-rest-sources
I hope this helps

Oracle Apex - how to calculate the sum of prices matching order id on one table and insert to another table

This question is a follow-up to another SO question.
This is for an e-commerce platform to show the end-user sales reports. I have a table ORDERS and a table ORDER_ITEMS. The data is received through a REST API as a JSON response. The JSON is synced through APEX REST Data Source and added to the local tables via a PLSQL trigger.
Sample JSON response
[
{
"order_id": "ZCHI8-N9Zm-VJ1o",
"order_number": 89653,
"order_date": "2023-01-01",
"store_id": 1,
"full_name": "Amalee Planque",
"email": "aplanque0#shinystat.com",
"city": "Houston",
"state": "Texas",
"zip_code": "77040",
"credit_card": "5048378382993155",
"order_items": [
{
"line_number": 1,
"product_id": 4,
"quantity": 1,
"price": 3919.8
},
{
"line_number": 2,
"product_id": 6,
"quantity": 1,
"price": 3089.36
},
{
"line_number": 3,
"product_id": 1,
"quantity": 1,
"price": 3474.4
}
]
},
{
"order_id": "XZnQx-PwYR-zWy2",
"order_number": 37946,
"order_date": "2022-01-29",
"store_id": 2,
"full_name": "Marillin Gadie",
"email": "mgadie1#comsenz.com",
"city": "Cleveland",
"state": "Ohio",
"zip_code": "44191",
"credit_card": "5108757233070957",
"order_items": [
{
"line_number": 1,
"product_id": 5,
"quantity": 1,
"price": 3184.37
}
]
},
]
Trigger to insert the order_items to the ORDER_ITEMS table
create or replace trigger "TR_MAINTAIN_LINES"
AFTER
insert or update or delete on "ORDERS_LOCAL"
for each row
begin
if inserting or updating then
if updating then
delete ORDER_ITEMS_LOCAL
where order_id = :old.order_id;
end if;
insert into ORDER_ITEMS_LOCAL ( order_id, line_id, line_number, product_id, quantity, price)
( select :new.order_id,
seq_line_id.nextval,
j.line_number,
j.product_id,
j.quantity,
j.price
from json_table(
:new.order_items,
'$[*]' columns (
line_id for ordinality,
line_number number path '$.line_number',
product_id number path '$.product_id',
quantity number path '$.quantity',
price number path '$.price' ) ) j );
elsif deleting then
delete ORDER_ITEMS_LOCAL
where order_id = :old.order_id;
end if;
end;
The ORDERS table contains all the order fields (order_id, order_number, etc.) It also contains order_items as a JSON array. (which gets extracted into the ORDER_ITEMS table by the trigger)
ORDERS table
ORDERS table data
The ORDER_ITEMS table contains all the order item fields (line_number, quantity, price). It also contains the order_id to reference which order the line item is referring to.
ORDER_ITEMS table
ORDER_ITEMS table data
I need to add an ORDER_TAX column to the ORDERS table. In which:
Grabs the order_items from the ORDER_ITEMS table which has the same order_id.
Adds all the price columns to get the total.
Multiply the total by 0.15 to simulate the estimated sales tax
Insert the 'sales tax' into the ORDER_TAX column in the ORDERS table.
I've created the ORDER_TAX column. I think I need to create a new trigger for this but I'm not quite sure how to code this out. I also need to create an ORDER_TOTAL column to the ORDERS table but I think I can figure that out once someone helps me with this initial question.
-----------UPDATE--------------
Per Koen's comment, it seems I need to actually create a view for this instead of a trigger.
The SQL query below returns the expected results
select ORDER_ID, SUM(PRICE) * 0.15 AS ORDER_TAX
from ORDER_ITEMS_LOCAL
GROUP BY ORDER_ID
How do I create a view so it inserts the value into the ORDERS table, ORDER_TAX column?
Per Koen's comment, I've been able to figure this out.
Go to Object Browser Create View
select
ORDERS_LOCAL.*,
A2.TAX,
A2.SUBTOTAL,
A2.TOTAL
from ORDERS_LOCAL
inner join (
select
ORDER_ID,
cast(sum(price) *0.15 as decimal(10,2)) as tax,
sum(price) as subtotal,
cast(sum(price) *1.15 as decimal(10,2)) as total
from ORDER_ITEMS_LOCAL
group by ORDER_ID
) A2
ON ORDERS_LOCAL.ORDER_ID=A2.ORDER_ID;
I then created a Master Detail report with the view as the source.

AWS AppSync delta table not working correctly

I am following aws appsync tutorial and i'm stuck at delta sync step (https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-delta-sync.html).
I have finished the example but the result is not as expected. In the update step, dynamodb does not create 2 records (a record when the item was created and record for when the item was updated) as in the example. And when using delta query, an error message is received:
"data": null,
"errors": [
{
"path": [
"syncPosts"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'PostConnection' within parent 'Query' (/syncPosts)"
}
]
}
My detal table ttl is 1 min, and delta queries only select from base table, not delta table.
Can someone look into this and help me out? Thanks
I found solution, i was setting the delta table partition_key field to ds_pk and delta table sort_key field to ds_sk. The result is as expected after the changed.

DynamoDB Query with key condition and filter expression by array contains

I want to query a DDB GSI with key condition, and apply filter on returned result using contains function.
Data I have in DDB table:
{
"lookupType": "PRODUCT_GROUP",
"name": "Spring framework study set",
"structureJson": {
"productIds": [
"FBCUPOQsrp",
"Y4LDaiViLY",
"J6N3UWq9CK"
]
},
"ownerId": "mT9R9y6zGO"
}
{
"lookupType": "PRODUCT_GROUP",
"name": "Relational databases study set",
"structureJson": {
"productIds": [
"QWQWQWQWQW",
"XZXZXZXZXZ"
]
},
"ownerId": "mT9R9y6zGO"
}
...
I have a compound GSI (ownerId - HASH, lookupType - RANGE).
When I try to query the DDB (query structure is in "2" field) I get the result (the result is in "3"):
{
"0":[
],
"2":{
"TableName":"Products",
"IndexName":"ProductsOwnerIdLookupTypeIndex",
"KeyConditionExpression":"#ownerId = :ownerId and #lookupType = :lookupType",
"FilterExpression":"contains(#structureMember, :memberId)",
"ExpressionAttributeNames":{
"#ownerId":"ownerId",
"#lookupType":"lookupType",
"#structureMember":"structureJson.productIds"
},
"ExpressionAttributeValues":{
":ownerId":"mT9R9y6zGO",
":lookupType":"PRODUCT_GROUP",
":memberId":"FBCUPOQsrp"
}
},
"3":{
"Items":[
],
"Count":0,
"ScannedCount":2
}
}
The returned result set is empty, despite I have data with given field values.
How I see the query (or what I want to achieve):
When I query the GSI with ownerId = mT9R9y6zGO and lookupType = PRODUCT_GROUP it will find 2 items - Spring and Relational DB sets
As the second step DDB will scan the returned query result with contains (structureJson.productIds, FBCUPOQsrp) filter expression and it should return one result to me, but I get empty set
Is something wrong with the query or do I miss some point in DDB query workflow?

Facebook ql asks for indexable column in where clause when it is indexable itself

I have tried the following FQL in Facebook:
SELECT checkin_id FROM checkin WHERE author_uid = me()
Test this query
However, I get the following response:
{
"error": {
"message": "Your statement is not indexable. The WHERE clause must contain an indexable column. Such columns are marked with * in the tables linked from http://developers.facebook.com/docs/reference/fql ",
"type": "NoIndexFunctionException",
"code": 604
}
}
But checkin is marked as indexable in the documentation.
Now what can be done?