Number of items in Redis set - count

What's the easiest way to getting the number (count) of items in Redis set? Preferably without the need to dump whole set and count the lines... So far, I have found only BITCOUNT, which I have not found that useful...

The SCARD command returns the cardinality (i.e. number of items) of a Redis set.
http://redis.io/commands/scard
There is a similar command (ZCARD) for sorted sets.

Also if you have a sorted set, you can use the ZCOUNT command to get the number of elements in the sorted set at key with a score between min and max.
Example:
ZCOUNT myzset 2 15
return count of elements with score >= 2 and <= 15

for python you can use scard.
lebgth=db.scard(key)

Related

Count with limit and offset in sqlite

am am trying to write a function in python to use sqlite and while I managed to get it to work there is a behavior in sqlite that I dont understand when using the count command. when I run the following sqlite counts as expected, ie returns an int.
SELECT COUNT (*) FROM Material WHERE level IN (?) LIMIT 10
however when I add, shown below, an offset to the end sqlite returns an emply list, in other words nothing.
SELECT COUNT (*) FROM Material WHERE level IN (?) LIMIT 10 OFFSET 82
while omitting the offset is an easy fix I don't understand why sqlite returns nothing. Is this the expected behavior for the command I gave?
thanks for reading
When you execute that COUNT(*) it will return you only a single row.
The LIMIT function is for limiting the number of rows returned. You are setting the limit to 10 which doesn't have any effect here (Because it is returning only a single row).
OFFSET is for offsetting/skipping specified number of rows. Which also doesn't have any effect here.
In simple terms your query translates to COUNT number of rows, then return 10 rows starting from 83rd position. Since you've a single row it will always return empty.
Read about LIMIT and OFFSET

Filter - Calculated fields relation in Tableau

I have 20 lists of servers. Suppose we have 50 servers and everyday (for 20 days) we get a list of active servers.
Having this list, I want to calculate the number of times each server has appeared in the lists. Suppose that Server1 has appeared in 16 out of these 20 lists. Here's how I'm doing it:
new calculated field: {FIXED [Server]:COUNT([Server])}
move this calculated field to columns
calculate CNTD (count distinct) and put it in rows
here's the results:
Now here comes the question:
What if I want to draw the very same chart, but only according to the last 5 lists (lists we've got the last 5 days)? If I filter based on paths and take the last 5 lists, the numbers calculated in calculated fields won't update. they're gonna still be 6,8,...16 while there are only 5 lists (the maximum number of appearance should be 5). Any ideas?
Instead of using the FIXED level-of-detail (LOD), use INCLUDE. The order of operations for LOD calculations will run FIXED calculations run before applying any filtering. INCLUDE/EXCLUDE are applied after filtering.
{INCLUDE [Server]:COUNT([Server])}
This image from the online help shows the order of operations for LOD calculations and filtering.
See https://onlinehelp.tableau.com/current/pro/desktop/en-us/calculations_calculatedfields_lod_overview.html for more details.

HOW TO SELECT RANDOM ROWS IN mb maria

SELECT col1 FROM tbl ORDER BY RAND() LIMIT 10;
This can work fine for small tables. However, for big table, it will have a serious performance problem as in order to generate the list of random rows, MySQL need to assign random number to each row and then sort them.
Even if you want only 10 random rows from a set of 100k rows, MySQL need to sort all the 100k rows and then, extract only 10 of them.
My solution for this problem, is to use RAND in the WHERE clause and not in the ORDER BY clause. First, you need to calculate the fragment of your desired result set rows number from the total rows in your table. Second, use this fragment in the WHERE clause and ask only for RAND numbers that smallest (or equal) from this fragment.
SELECT col1 FROM tbl WHERE RAND()<=0.0005;
In order to get exactly 100 row in the result set, we can increase the fragment number a bit and limit the query:
For example:

Get more than 1000 observation for View() function

I'm working with RStudion and when I use the View() command I can only see the first 1000 rows.
Is it possible to get more than 1000 rows ? or less than 1000 rows ? is it possible to get a range observation like observation in 500 : 1000 ?
Thanks in advance.
It can happen:
Let's say that you have a data.frame called df which has 2000 rows and 2 columns. To view all of them you need to explicitly type on the console:
utils::View(df) #however this will open a new separate window to view all the records.
In order to view just 500:1000 records of the data.frame just do:
utils::View(df[500:1000,]) #i.e. use the standard way of slicing the data.frame in the function.
Hope this helps.

In Graphite, how can count the number of data points above a value?

I was wondering if there was a Graphite function or a way of getting the number of points above a certain value. Let's say I have 2,44,24,522,52,534 for the same time and I want to get the number of points over 40, in this case it would be 3. Any tips?
Thanks
You can use removeBelowValue(your.metric, 40) to only display points above 40.
Then use something to make non zero value equal to 1 (I am thinking of pow(_, 0) but I am not sure of how it behaves with None values given by removeBelowValue). If you use recent (>0.9.x) version of graphite, you can use isNonNull instead of pow
In the end you can use any function to summarize your the 1s you have, summarize you should be good. You only have to select your range.
Suggestion : summarize(pow(removeBelowValue(your.metric,40),0), '1hour', 'sumSeries')

Resources