MariaDB Columnstore delete doesn't delete - mariadb

Every night I have a cron job that runs to remove some record from a table in columnstore table. Since a few days the delete request does not delete anymore. yet when I execute manually it seems OK, but no :
select count(*) from TABLE where FIELD1 <> 0 and FIELD2 = 0;
count(*)
159040
1 row in set (0.11 sec)
delete from TABLE where FIELD1 <> 0 and FIELD2 = 0 LIMIT 20000;
Query OK, 20000 rows affected (6.00 sec)
select count(*) from TABLE where FIELD1 <> 0 and FIELD2 = 0;
count(*)
159040
1 row in set (0.10 sec)
As you can see above, delete query seems OK but no rows have been removed. :-(
A little help would be welcome
Thanks you all :)
*Mariadb Columnstore version 1.1.2

DELETE in CS is effectively an UPDATE. Did you try to update one record? Does it work with LIMIT applied?
I would suggest you to try the latest 1.2.5 though.

Related

Create trigger on delete that resets auto increment id if the table is empty on sqlite

I want to create a trigger that will reset the auto increment id to 0 if the table have just become empty. I've tried the following:
CREATE TRIGGER reset_autoincrement AFTER DELETE ON temp WHEN count(*) = 0
BEGIN
UPDATE sqlite_sequence SET seq = 0 WHERE name = 'temp';
END
Although the SQL seems correct, it doesn't do what I want.
Any suggestions?
The expression in the WHEN clause is not in the context of the table; count(*) without a table returns 1.
Use a subquery instead:
... WHEN (SELECT count(*) FROM temp) = 0 ..

how to make conditional ordering in sqlite?

I have table with fields task_priority, task_completed_time,task_completed. Completed colume have values 0 and 1 and it is a primary sort. I want to make secondary sort to be priority if completed is 0 and completed_time if completed is 1. How can i get it?
after some typo fixes i made it:
SELECT * FROM task_table
WHERE (task_name LIKE ? )
ORDER BY task_completed ASC,
CASE task_completed
WHEN 0 THEN task_priority
WHEN 1 THEN task_completed_time
END DESC

convert t-sql statement to teradata

I am trying to understand the equivalent of this statement
IF OBJECT_ID('Current') IS NOT NULL
DROP TABLE Current;
in Teradata.
Can someone help me out converting this statement to TD14. Thank you!
You can do this in at least newer versions of TD:
select
count (*)
from
dbc.tablesv where tablename = '<your table>'
and databasename = '<your db>'
having count (*) > 0;
.if activitycount = 1 then .GOTO DropTable;
.if activitycount <> 1 then .quit;
.LABEL DropTable
select 'DROP TABLE!';
drop table <your db>.<your table>;
Sadly enough, this won't work with volatile tables. If they are global temporary tables, you can use
select
count (*)
from
dbc.AllTempTablesVX where B_tablename =

Is it possible to use WHERE clause in same query as PARTITION BY?

I need to write SQL that keeps only the minimum 5 records per each identifiable record in a table. For this, I use partition by and delete all records where the value returned is greater than 5. When I attempt to use the WHERE clause in the same query as the partition by statement, I get the error "Ordered Analytical Functions not allowed in WHERE Clause". So, in order to get it to work, I have to use three subqueries. My SQL looks ilke this:
delete mydb.mytable where (field1,field2) in
(
select field1,field2 from
(
select field1,field2,
Rank() over
(
partition BY field1
order by field1,field2
) n
from mydb.mytable
) x
where n > 5
)
The innermost subquery just returns the raw data. Since I can't use WHERE there, I wrapped it with a subquery, the purpose of which is to 1) use WHERE to get records greater than 5 in rank and 2) select only field1 and field2. The reason why I select only those two fields is so that I can use the IN statement for deleting those records in the outermost query.
It works, but it appears a bit cumbersome. I'd like to consolidate the inner two subqueries into a single subquery. Is this possible?
Sounds like you need to use the QUALIFY clause which is the HAVING clause for Window Aggregate functions. Below is my take on what you are trying to accomplish.
Please do not run this SQL directly against your production data without first testing it.
/* Physical Delete */
DELETE TGT
FROM MyDB.MyTable TGT
INNER JOIN
(SELECT Field1
, Field2
FROM MyDB.MyTable
QUALIFY ROW_NUMBER() (PARTITION BY Field1, ORDER BY Field1,2)
> 5
) SRC
ON TGT.Field1 = SRC.Field1
AND TGT.Field2 = SRC.Fileld2
/* Logical Delete */
UPDATE TGT
FROM MyDB.MyTable TGT
,
(SELECT Field1
, Field2
FROM MyDB.MyTable
QUALIFY ROW_NUMBER() (PARTITION BY Field1, ORDER BY Field1,2)
> 5
) SRC
SET Deleted = 'Y'
/* RecordExpireDate = Date - 1 */
WHERE TGT.Field1 = SRC.Field1
AND TGT.Field2 = SRC.Fileld2

Update table using cursor but also update records in another table

I'm updating the IDs with new IDs, but I need to retain the same ID for the master record in table A and its dependants in table B.
The chunk bracketed by comments is the part I can't figure out. I need to update all the records in table B that share the same ID with the current record I'm looking at for table A.
DECLARE CURSOR_A CURSOR FOR
SELECT * FROM TABLE_A
FOR UPDATE
OPEN CURSOR_A
FETCH NEXT FROM CURSOR_A
WHILE ##FETCH_STATUS = 0
BEGIN
BEGIN TRANSACTION
UPDATE KEYMASTERTABLE
SET RUNNING_NUMBER=RUNNING_NUMBER+1
WHERE TRANSACTION_TYPE='TABLE_A_NEXT_ID'
-- FOLLOWING CHUNK IS WRONG!!!
UPDATE TABLE_B
SET TABLE_B_ID=(SELECT RUNNING_NUMBER
FROM KEYMASTERTABLE WHERE TRANSACTION_TYPE='TABLE_A_NEXT_ID')
WHERE TABLE_B_ID = (SELECT TABLE_A_ID
FROM CURRENT OF CURSOR A)
-- END OF BAD CHUNK
UPDATE TABLE_A
SET TABLE_A_ID=(SELECT RUNNING_NUMBER
FROM KEYMASTERTABLE WHERE TRANSACTION_TYPE='TABLE_A_NEXT_ID')
WHERE CURRENT OF CURSOR_A
COMMIT
FETCH NEXT FROM CURSOR_A
END
CLOSE CURSOR_A
DEALLOCATE CURSOR_A
GO
Based on the assumption that this process of incrementing current data by +1 doesn't cause issues in the data itself, I would create a translation table.
Column1 would be the old ID, Column2 would be the new ID.
Both tables would be run through the same update then.
This also gives you auditing on the process, in case something goes wrong.
Something like
Update table TargetA a
set a.id =(select t.column2 from tranlation_table t where t.column1 = a.id);
Update table TargetB b
set b.id =(select t.column2 from tranlation_table t where t.column1 = b.id)

Resources