Get last index rebuild date in sybase - sybase

Does anyone know of a way to query the date a sybase index was last rebuild?

This seems to be the only way, the create date of the index:
select name, id, crdate from sysindexes where id = object_id('TableName')

Related

Row HASHBYTES in results

I wonder if anyone can help me achieve this. I have a row of 100 columns, 1st being the primary key. I need to retrieve from each row in my query the primary key column and a HASHBYTES of the entire row, so the results would look like:
PK Hash
000123 HASHVALUE1234567890ETC
I can get the entire row with this select statement:
SELECT hashbytes('sha1', (SELECT * From Table Where PK = 000123 FOR XML RAW))
I cannot however work out how to get the PK and hash together in the same results..
Thanks in advance for your help.
Derek.
Select CompanyPK,
(SELECT hashbytes('sha1',
(SELECT * From Company
Where CompanyPK = 'f5dba28b-ae3b-407a-807c-068acde88298' FOR XML RAW)))
as [Hash]
From Company
Where CompanyPK = 'f5dba28b-ae3b-407a-807c-068acde88298'
Sorry to waste your time :)

Database Index when SQL statement includes "IN" clause

I have SQL statement which takes really a lot of time to execute and I really had to improve it somehow.
select * from table where ID=1 and GROUP in
(select group from groupteam where
department= 'marketing' )
My question is if I should create index on columns ID and GROUP would it help?
Or if not should I create index on second table on column DEPARTMENT?
Or I should create two indexes for both tables?
First table has 249003.
Second table has in total 900 rows while query in that table returns only 2 rows.
That is why I am surprised that response is so slow.
Thank you
You can also use EXISTS, depending on your database like so:
select * from table t
where id = 1
and exists (
select 1 from groupteam
where department = 'marketing'
and group = t.group
)
Create a composite index on individual indexes on groupteam's department and group
Create a composite index or individual indexes on table's id and group
Do an explain/analyze depending on your database to review how indexes are being used by your database engine.
Try a join instead:
select * from table t
JOIN groupteam gt
ON d.group = gt.group
where ID=1 AND gt.department= 'marketing'
Index on table group and id column and table groupteam group column would help too.

find the column identity in a table in sql server

Can someone know if there is a way to find the column name that has the identity property in an sql server table
Thanks in advance
You can use sys.columns for this quite easily.
select *
from sys.columns
where object_id = object_id('YourTableNameHere')
AND is_identity = 1
As an alternative to looking it up in the metadata, you can use $identity to refer to an identity column. For example, given a table:
CREATE TABLE test_table (some_random_name INT IDENTITY, other_column VARCHAR(20));
...you can use:
SELECT $identity, other_column FROM test_table;
to bring back some_random_name, other_column.
($identity replaces the deprecated IDENTITYCOL of earlier versions of SQL Server.)
You can use the solution in this link to search the whole database for a specific value, which can be a username you know exists "somewhere" in the database.
It gives you the table and column name where that specific value exists.

SQL Server : query help on how to find oldest value?

I have a table called dbo.files with a column created_time that contain values like:
2012-05-21 13:28:56.960
This tables has over 138 million rows.
I would like to find the oldest value in the table within the created_time column. This would tell me when I first started writing to the table.
What I have tried so far as a test
select count(*)
from dbo.files
where created_time < '2010-01-01 00:00:00.000'
this comes back with 0
How can I find the oldest value in that column?
SELECT min(created_time)
FROM dbo.Files

Select newest datetime for each userid MS Sql Server

I have a table used for a chat. Among others there is a field called userid and a field called timesent. I need to know the latest timesent for each userid in the table, so that I can delete them from the table if they haven't said anything for 3 minutes, in which case I will assume they are gone.
I can't really crack this nut... How do I query.
I could of course split it up and first select all the userids and then loop through them and select top 1 timesent in my method, but I was wondering if sql alone can do the trick, so I don't need to execute tons of queries.
To get the latest timesent per userid you can use MAX
SELECT userid, MAX(timesent) AS timesent
FROM your_table
GROUP BY userid
Or to do the specified delete you can use
DELETE your_table
FROM your_table y1
WHERE NOT EXISTS(SELECT *
FROM your_table y2
WHERE y2.userid = y1.userid
AND y2.timesent >= DATEADD(MINUTE, -3, GETDATE()))

Resources