Insert:
INSERT INTO MATCHES ("url", "league", "home_team", "away_team", "current_time", "current_score", "penalty_not_score_team", "penalty_not_score_time", "penalty_not_score_name")
VALUES ('https://www.myscore.com.ua/match/8nsN4E4k/#match-summary', 'АРГЕНТИНА: Кубок Аргентины - 1/16', 'Химнасия Мендоса', 'Ривер Плейт', 'Послес.п.', '1 - 2', 'home', '4''', 'Andrada B.')
Result:
Result: query completed successfully
At line 1:
INSERT INTO MATCHES ("url", "league", "home_team", "away_team", "current_time", "current_score", "penalty_not_score_team", "penalty_not_score_time", "penalty_not_score_name")
VALUES ('https://www.myscore.com.ua/match/8nsN4E4k/#match-s...', 'АРГЕНТИНА: Кубок Аргентины - 1/16', 'Химнасия Мендоса', 'Ривер Плейт', 'Послес.п.', '1 - 2', 'home', '4''', 'Andrada B.')
Select:
SELECT *
FROM MATCHES
WHERE (url="https://www.myscore.com.ua/match/8nsN4E4k/#match-summary" AND league="АРГЕНТИНА: Кубок Аргентины - 1/16" AND home_team="Химнасия Мендоса" AND away_team="Ривер Плейт" AND current_time="Послес.п." AND current_score="1 - 2" AND penalty_not_score_team="home" AND penalty_not_score_time="4'" AND penalty_not_score_name="Andrada B.")
Result:
Result: 0 rows returned in 1ms
At line 1:
SELECT *
FROM MATCHES
WHERE (url="https://www.myscore.com.ua/match/8nsN4E4k/#match-s..." AND league="АРГЕНТИНА: Кубок Аргентины - 1/16" AND home_team="Химнасия Мендоса" AND away_team="Ривер Плейт" AND current_time="Послес.п." AND current_score="1 - 2" AND penalty_not_score_team="home" AND penalty_not_score_time="4'" AND penalty_not_score_name="Andrada B.")
The record in the database is 100%, I watch it with DB Browser for SQLite3
I tried different syntaxes - to no avail.
Short queries work, but I need to sample all items.
You have to enquote current_time otherwise it is treated as function call SELECT current_time:
AND current_time="Послес.п."
=>
AND "current_time"='Послес.п.'
db<>fiddle demo
CREATE TABLE t(current_time CHAR(10));
INSERT INTO t(current_time) VALUES('aaa');
-- 1 row
SELECT * FROM t;
-- 0 rows returned
SELECT *
FROM t
WHERE current_time = 'aaa';
-- 1 row returned
SELECT *
FROM t
WHERE "current_time" = 'aaa';
Related
I have two tables which I export from my video editing suite, one ("MediaPool") containing a row for each media file imported into the project, another ("Montage") for the portions of that file used in a specific edit. The fields that are associated between the two are MediaPool.FileName and Montage.Name, which are very similar (Filename only adds the file extension).
# MediaPool
Filename | Take
---------------------------------
somefile.mp4 | Getty
file2.mov | Associated Press
file3.mov | Associated Press
and
# Montage
Name | RecordIn | RecordOut
------------------------------------------
somefile | 01:01:01:01 | 01:01:20:19
somefile | 01:05:15:23 | 01:05:16:10
somefile | 01:25:19:10 | 01:30:16:04
file2 | 01:30:11:10 | 01:31:18:12
file2 | 01:40:15:22 | 01:42:21:17
The tables contain many more columns of course, but only the above is relevant.
Only the "MediaPool" table contains the field called "Take" which designates the file's copyright holder (long story). It can't be included in the "Montage" export. I needed to calculate the total duration of footage used from each source, by subtracting the RecordIn timecode from RecordOut and adding each result. This turned out to be more complicated than I expected, as I have some notions of programming but almost none when it comes to SQL (sqlite in my case).
I managed to come up with the following, which works fine and runs in under 4 seconds. However, from the little programming I've done, it seems overlong and very inelegant. Is there a shorter way to achieve this?
BTW, I'm using 25 fps timecode and I can't use LPAD in sqlite.
SELECT
Source,
SUBSTR('00' || CAST(DurationFrames/(60*60*25) AS TEXT), -2, 2) || ':' ||
SUBSTR('00' || CAST(DurationFrames%(60*60*25)/(60*25) AS TEXT), -2, 2) || ':' ||
SUBSTR('00' || CAST(DurationFrames%(60*60*25)%(60*25)/25 AS TEXT), -2, 2) || ':' ||
SUBSTR('00' || CAST(DurationFrames%(60*60*25)%(60*25)%25 AS TEXT), -2, 2)
AS DurationTC
FROM
(
SELECT
MediaPool.Take AS Source,
Montage.RecordIn,
Montage.RecordOut,
SUM(CAST(SUBSTR(Montage.RecordOut, 1, 2) AS INT)*3600*25 +
CAST(SUBSTR(Montage.RecordOut, 4, 2) AS INT)*60*25 +
CAST(SUBSTR(Montage.RecordOut, 7, 2) AS INT)*25 +
CAST(SUBSTR(Montage.RecordOut, 10, 2) AS INT) -
CAST(SUBSTR(Montage.RecordIn, 1, 2) AS INT)*3600*25 -
CAST(SUBSTR(Montage.RecordIn, 4, 2) AS INT)*60*25 -
CAST(SUBSTR(Montage.RecordIn, 7, 2) AS INT)*25 -
CAST(SUBSTR(Montage.RecordIn, 10, 2) AS INT))
AS DurationFrames
FROM
MediaPool
JOIN
Montage ON MediaPool.FileName LIKE '%' || Montage.Name || '%'
GROUP BY
Take
ORDER BY
Take
)
Here's a simplified query that produces the same results as yours on your test data. Mostly it uses printf() instead of a bunch of string concatenation and substr()s, and uses strftime() to calculate the total seconds of the hours minutes seconds part of the timecode:
WITH frames AS
(SELECT Take, sum((strftime('%s', substr(RecordOut,1,8))*25 + substr(RecordOut,10))
- (strftime('%s', substr(RecordIn,1,8))*25 + substr(RecordIn,10)))
AS DurationFrames
FROM MediaPool
JOIN Montage ON MediaPool.Filename LIKE Montage.Name || '.%'
GROUP BY Take)
SELECT Take AS Source
, printf("%02d:%02d:%02d:%02d", DurationFrames/(60*60*25),
DurationFrames%(60*60*25)/(60*25),
DurationFrames%(60*60*25)%(60*25)/25,
DurationFrames%(60*60*25)%(60*25)%25)
AS DurationTC
FROM frames
ORDER BY Take;
I have created a table with a collection. Inserted a record and took sstabledump of it and seeing there is range tombstone for it in the sstable. Does this tombstone ever get removed? Also when I run sstablemetadata on the only sstable, it shows "Estimated droppable tombstones" as 0.5", Similarly it shows one record with epoch time as insert time for - "Estimated tombstone drop times: 1548384720: 1". Does it mean that when I do sstablemetadata on a table having collections, the estimated droppable tombstone ratio and drop times values are not true and dependable values due to collection/list range tombstones?
CREATE TABLE ks.nmtest (
reservation_id text,
order_id text,
c1 int,
order_details map<text, text>,
PRIMARY KEY (reservation_id, order_id)
) WITH CLUSTERING ORDER BY (order_id ASC)
user#cqlsh:ks> insert into nmtest (reservation_id , order_id , c1, order_details ) values('3','3',3,{'key':'value'});
user#cqlsh:ks> select * from nmtest ;
reservation_id | order_id | c1 | order_details
----------------+----------+----+------------------
3 | 3 | 3 | {'key': 'value'}
(1 rows)
[root#localhost nmtest-e1302500201d11e983bb693c02c04c62]# sstabledump mc-5-big-Data.db
WARN 02:52:19,596 memtable_cleanup_threshold has been deprecated and should be removed from cassandra.yaml
[
{
"partition" : {
"key" : [ "3" ],
"position" : 0
},
"rows" : [
{
"type" : "row",
"position" : 41,
"clustering" : [ "3" ],
"liveness_info" : { "tstamp" : "2019-01-25T02:51:13.574409Z" },
"cells" : [
{ "name" : "c1", "value" : 3 },
{ "name" : "order_details", "deletion_info" : { "marked_deleted" : "2019-01-25T02:51:13.574408Z", "local_delete_time" : "2019-01-25T02:51:13Z" } },
{ "name" : "order_details", "path" : [ "key" ], "value" : "value" }
]
}
]
}
SSTable: /data/data/ks/nmtest-e1302500201d11e983bb693c02c04c62/mc-5-big
Partitioner: org.apache.cassandra.dht.Murmur3Partitioner
Bloom Filter FP chance: 0.010000
Minimum timestamp: 1548384673574408
Maximum timestamp: 1548384673574409
SSTable min local deletion time: 1548384673
SSTable max local deletion time: 2147483647
Compressor: org.apache.cassandra.io.compress.LZ4Compressor
Compression ratio: 1.0714285714285714
TTL min: 0
TTL max: 0
First token: -155496620801056360 (key=3)
Last token: -155496620801056360 (key=3)
minClustringValues: [3]
maxClustringValues: [3]
Estimated droppable tombstones: 0.5
SSTable Level: 0
Repaired at: 0
Replay positions covered: {CommitLogPosition(segmentId=1548382769966, position=6243201)=CommitLogPosition(segmentId=1548382769966, position=6433666)}
totalColumnsSet: 2
totalRows: 1
Estimated tombstone drop times:
1548384720: 1
Another quuestion was on the nodetool tablestats output - what does slice refer to in cassandra?
Average live cells per slice (last five minutes): 1.0
Maximum live cells per slice (last five minutes): 1
Average tombstones per slice (last five minutes): 1.0
Maximum tombstones per slice (last five minutes): 1
Dropped Mutations: 0
sstablemetadata does not have the information about your table that is not held within the sstable as it is not guaranteed to be run on system that has Cassandra running, and even if it was its very complex to be able to know how to pull the schema information from it.
Since the gc_grace_seconds is a table parameter and not in the metadata it defaults to assuming a 0 gc grace so the droppable times listed in that histogram will be more a histogram of the tombstone creation times by default. If you know your gc grace you can add it as a -g parameter to your sstablemetadata call. like:
sstablemetadata -g 864000 mc-5-big-Data.db
see http://cassandra.apache.org/doc/latest/tools/sstable/sstablemetadata.html for information on the tools output.
With collections it's just normal range tombstone with all that it entails. They are used to prevent the requirement of a read-before-write when overwriting the value of a multicell collection.
I have the below PeopleCode step in an Application Engine program that reads a CSV file using a File Layout and then inserts the data into a table, and I am just trying to get a better understanding of how the the line of code (&SQL1 = CreateSQL("%Insert(:1)");) in the below script gets generated. It looks like the CreateSQL is using a bind variable (:1) inside the Insert statement, but I am struggling as where to find where this variable is defined in the program.
Function EditRecord(&REC As Record) Returns boolean;
Local integer &E;
&REC.ExecuteEdits(%Edit_Required + %Edit_DateRange + %Edit_YesNo + %Edit_OneZero);
If &REC.IsEditError Then
For &E = 1 To &REC.FieldCount
&MYFIELD = &REC.GetField(&E);
If &MYFIELD.EditError Then
&MSGNUM = &MYFIELD.MessageNumber;
&MSGSET = &MYFIELD.MessageSetNumber;
&LOGFILE.WriteLine("****Record:" | &REC.Name | ", Field:" | &MYFIELD.Name);
&LOGFILE.WriteLine("****" | MsgGet(&MSGSET, &MSGNUM, ""));
End-If;
End-For;
Return False;
Else
Return True;
End-If;
End-Function;
Function ImportSegment(&RS2 As Rowset, &RSParent As Rowset)
Local Rowset &RS1, &RSP;
Local string &RecordName;
Local Record &REC2, &RECP;
Local SQL &SQL1;
Local integer &I, &L;
&SQL1 = CreateSQL("%Insert(:1)");
rem &SQL1 = CreateSQL("%Insert(:1) Order by COUNT_ORDER");
&RecordName = "RECORD." | &RS2.DBRecordName;
&REC2 = CreateRecord(#(&RecordName));
&RECP = &RSParent(1).GetRecord(#(&RecordName));
For &I = 1 To &RS2.ActiveRowCount
&RS2(&I).GetRecord(1).CopyFieldsTo(&REC2);
If (EditRecord(&REC2)) Then
&SQL1.Execute(&REC2);
&RS2(&I).GetRecord(1).CopyFieldsTo(&RECP);
For &L = 1 To &RS2.GetRow(&I).ChildCount
&RS1 = &RS2.GetRow(&I).GetRowset(&L);
If (&RS1 <> Null) Then
&RSP = &RSParent.GetRow(1).GetRowset(&L);
ImportSegment(&RS1, &RSP);
End-If;
End-For;
If &RSParent.ActiveRowCount > 0 Then
&RSParent.DeleteRow(1);
End-If;
Else
&LOGFILE.WriteRowset(&RS);
&LOGFILE.WriteLine("****Correct error in this record and delete all error messages");
&LOGFILE.WriteRecord(&REC2);
For &L = 1 To &RS2.GetRow(&I).ChildCount
&RS1 = &RS2.GetRow(&I).GetRowset(&L);
If (&RS1 <> Null) Then
&LOGFILE.WriteRowset(&RS1);
End-If;
End-For;
End-If;
End-For;
End-Function;
rem *****************************************************************;
rem * PeopleCode to Import Data *;
rem *****************************************************************;
Local File &FILE1, &FILE3;
Local Record &REC1;
Local SQL &SQL1;
Local Rowset &RS1, &RS2;
Local integer &M;
&FILE1 = GetFile("\\nt115\apps\interface_prod\interface_in\Item_Loader\ItemPriceFile.csv", "r", "a", %FilePath_Absolute);
&LOGFILE = GetFile("\\nt115\apps\interface_prod\interface_in\Item_Loader\ItemPriceFile.txt", "r", "a", %FilePath_Absolute);
&FILE1.SetFileLayout(FileLayout.GH_ITM_PR_UPDT);
&LOGFILE.SetFileLayout(FileLayout.GH_ITM_PR_UPDT);
&RS1 = &FILE1.CreateRowset();
&RS = CreateRowset(Record.GH_ITM_PR_UPDT);
REM &SQL1 = CreateSQL("%Insert(:1)");
&SQL1 = CreateSQL("%Insert(:1)");
/*Skip Header Row: The following line of code reads the first line in the file layout (the header)
and does nothing. Then the pointer goes to the next line in the file and starts using the
file.readrowset*/
&some_boolean = &FILE1.ReadLine(&string);
&RS1 = &FILE1.ReadRowset();
While &RS1 <> Null
ImportSegment(&RS1, &RS);
&RS1 = &FILE1.ReadRowset();
End-While;
&FILE1.Close();
&LOGFILE.Close();
The :1 is coming from the line further down &SQL1.Execute(&REC2);
&REC2 gets assigned a record object, so the line &SQL1.Execute(&REC2); evaluates to %Insert(your_record_object)
Here is a simple example that's doing basically the same thing
Here is a description of %Insert
Answer because too long to comment:
The table name is most likely (PS_)GH_ITM_PR_UPDT. The general consensus is to name the FileLayout the same as the record it is based on.
If not, it is defined in FileLayout.GH_ITM_PR_UPDT. Open the FileLayout, right click the segment and under 'Selected Node Properties' you will find the 'File Record Name'.
In your code this record is carried over into &RS1.
&FILE1.SetFileLayout(FileLayout.GH_ITM_PR_UPDT);
&RS1 = &FILE1.CreateRowset();
The rowset is a collection of rows. A row consists of records and a record is a row of data from a database table. (Peoplesoft Object Data Types are fun...)
This rowset is filled with data in the following statement:
&RS1 = &FILE1.ReadRowset();
This uses your file as input and outputs a rowset collection, mapping the data to records based on how you defined your FileLayout.
The result is fed into the ImportSegment function:
ImportSegment(&RS1, &RS);
Function ImportSegment(&RS2 As Rowset, &RSParent As Rowset)
&RS2 in the function is a reference to &RS1 in the rest of your code.
The table name is also hidden here:
&RecordName = "RECORD." | &RS2.DBRecordName;
So if you can't/don't want to check the FileLayout, you could output &RS2.DBRecordName with a messagebox and your answer will be Message Log of your Process Monitor.
Finally a record object is created for this database table and it is filled with a row from the rowset. This record is inserted into the database table:
&REC2 = CreateRecord(#(&RecordName));
&RS2(&I).GetRecord(1).CopyFieldsTo(&REC2);
&SQL1 = CreateSQL("%Insert(:1)");
&SQL1.Execute(&REC2);
TLDR:
Table name can be found in the FileLayout or output in the ImportSegment Function as &RS2.DBRecordName
I am using Graticule to find people within a distance of x miles from an event. I need to send mail to everyone who is within x miles of the event.
Here is my code :-
user.rb
def self.weekly_update
#users = User.all
#users.each do |u|
#events = Event.all_with_distance([u.geo_lat, u.geo_lng]).where("start > ?", Time.zone.now).where("distance < 15")
UsersMailer.weekly_mail(u.email, #events).deliver
end
end
def self.all_with_distance(origin)
distance_sql = sql_for_distance(origin)
select("#{table_name}.*, #{distance_sql} AS distance").select("`locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name").joins(:location)
end
geo_search.rb
module GeoSearch
def sql_for_distance(origin)
Graticule::Distance::Spherical.to_sql(
:latitude => origin[0],
:longitude => origin[1],
:latitude_column => "`locations`.`geo_lat`",
:longitude_column => "`locations`.`geo_lng`",
:units => :kilometers
)
end
end
end
This is the error which I am getting :-
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/runner.rb:49:in `eval': Mysql2::Error: Unknown column 'distance' in 'where clause': SELECT events.*, (ACOS( SIN(RADIANS(26.8465108)) * SIN(RADIANS(`locations`.`geo_lat`)) + COS(RADIANS(26.8465108)) * COS(RADIANS(`locations`.`geo_lat`)) * COS(RADIANS(`locations`.`geo_lng`) - RADIANS(80.9466832)) ) * 6378.135) AS distance, `locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name FROM `events` INNER JOIN `locations` ON `locations`.`id` = `events`.`location_id` WHERE (start > '2011-12-10 10:38:20') AND (distance < 15) (ActionView::Template::Error)
If I remove .where("distance < 15") to .order("distance") everything works fine.
In both standard SQL and the dialect supported by MySQL, column aliases can't be used in a WHERE clause; you must use the column. Here's what it would look like for your code, placing the call to where in all_with_distance:
select("#{table_name}.*, #{distance_sql} AS distance")
.select("`locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name")
.joins(:location)
.where("#{distance_sql} < 50")
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Slow Performance of Sql Query
Hi,
I have asked the performance of query and i tried to simplyfy it.but still it not works.I am adding my query below.Please can you simplify it more effectively
select
r.parent_itemid f_id,
parent_item.name f_name,
parent_item.typeid f_typeid,
parent_item.ownerid f_ownerid,
parent_item.created f_created,
parent_item.modifiedby f_modifiedby,
parent_item.modified f_modified,
pt.name f_tname,
child_item.id i_id,
t.name i_tname,
child_item.typeid i_typeid,
child_item.name i_name,
child_item.ownerid i_ownerid,
child_item.created i_created,
child_item.modifiedby i_modifiedby,
child_item.modified i_modified,
r.ordinal i_ordinal
from
item child_item,
type t,
relation r,
item parent_item,
type pt
where
r.child_itemid = child_item.id and
t.id=child_item.typeid and
parent_item.id = r.parent_itemid and
pt.id = parent_item.typeid
and parent_item.id in (
select
itemid
from
permission
where
itemid=parent_item.id and
(holder_itemid in (10,100) and level > 0) )
order by
r.parent_itemid,
r.relation_typeid,
r.ordinal
Thanks you
regards
jennie
You don't need the correlated subquery on the permissions. Any other problems need to be fixed by checking the indexes indexes on the join fields (like item.child_itemid ) & filter fields (like permission.holder_itemid) will help the performance of your query
select
r.parent_itemid f_id,
parent_item.name f_name,
parent_item.typeid f_typeid,
parent_item.ownerid f_ownerid,
parent_item.created f_created,
parent_item.modifiedby f_modifiedby,
parent_item.modified f_modified,
pt.name f_tname,
child_item.id i_id,
t.name i_tname,
child_item.typeid i_typeid,
child_item.name i_name,
child_item.ownerid i_ownerid,
child_item.created i_created,
child_item.modifiedby i_modifiedby,
child_item.modified i_modified,
r.ordinal i_ordinal
from
item child_item,
type t,
relation r,
item parent_item,
type pt,
permission p
where
r.child_itemid = child_item.id
and t.id=child_item.typeid
and parent_item.id = r.parent_itemid
and pt.id = parent_item.typeid
and parent_item.id = p.itemid
and p.holder_itemid in (10, 100)
and p.level > 0
order by
r.parent_itemid,
r.relation_typeid,
r.ordinal
Try removing the subquery, something like:
select
r.parent_itemid f_id,
parent_item.name f_name,
parent_item.typeid f_typeid,
parent_item.ownerid f_ownerid,
parent_item.created f_created,
parent_item.modifiedby f_modifiedby,
parent_item.modified f_modified,
pt.name f_tname,
child_item.id i_id,
t.name i_tname,
child_item.typeid i_typeid,
child_item.name i_name,
child_item.ownerid i_ownerid,
child_item.created i_created,
child_item.modifiedby i_modifiedby,
child_item.modified i_modified,
r.ordinal i_ordinal
from
item child_item,
type t,
relation r,
item parent_item,
type pt,
permission perm /* <<< added this line <<< */
where
r.child_itemid = child_item.id and
t.id=child_item.typeid and
parent_item.id = r.parent_itemid and
pt.id = parent_item.typeid
and parent_item.id = perm.itemid /* <<< modified this line <<< */
and perm.itemid = parent_item.id /* <<< copied these 2 lines from the subquery <<< */
and (perm.holder_itemid in (10,100) and perm.level > 0) ) /* <<< */
order by
r.parent_itemid,
r.relation_typeid,
r.ordinal
Give it a try, and see if it works and makes any difference.