In App Engine NDB, I am querying entities that have a repeated property. I would like to order the result by the length of the array representing that property.
What I wish I could do:
Entity.query(...).order(len(Entity.repeatedProp))
You'll need to add an ndb.IntegerProperty() to your entity where you will store the length of the repeated property. Every time you change your repeated property, you'll need to update the stored length. Then you sort based on that stored length.
You could probably use a computed property, but I've never used one of those so I'm not sure.
Depending on how many entities are you sorting you can sort it by code.
Related
In my app I need to do fast Query but I don't know which is faster
materials = Material.objects.only('name')
Or do filter this in view
materials = Material.objects.all()
And then use for loop to show list of items from 'name' row
I think that first is better or there is better way to do this?
It cant be done with filter() because it need to show all of fields in this row.
If you only want the names, you can use a .values_list(..):
materials = list(Material.objects.values_list('name', flat=True))
This will avoid wrapping the records in Material objects. That being said, unless some of the columns contain (very) large amounts of data, using .only(..) will not significantly speed up the process. Furthermore software design-wise it is often better to fetch Material objects, since that means that you can define behavior in your Material model.
I have a mongodb collection with close to 100,000 records and each record has around 5000 keys. Lot of this is empty. How can I find (maybe visually represent) this emptiness of data.
In other words, I would like to analyze the type of values in each key. What would be the right approach for this.
You could take a look to MongoDB aggregation strategies. Check out $group.
From how you exposed your problem, I could totally see an accumulator over the number of keys of each record.
As an example, with the appropriate thresholds and transformation, such an operation could basically return you the records grouped by number of keys (or an array solely populated with the number of keys for each record).
Such an approach could also allow you to perform some data analysis over the keys used for each record.
I have a queryset of all objects of a model. Iterating over the objects, I am removing updating rows with value repetition in a column. So without having to hit the database again, i want to remove the updated rows from the bigger queryset.
What is the most efficient way to do this?
You may need to provide more information about your specific use case, but in general, Django defers the evaluation of QuerySets until they are actually needed. If you are able to construct a QuerySet of the exclusion set independently of the larger QuerySet, you can call the .exclude() method and generate one large query instead of two smaller ones. For example:
excluded_set = Model.objects.filter(...)
large_set = Model.objects.filter().exclude(id__in=excluded_set)
I'm not sure if this has been answered before, I didn't get anything on a quick search.
My table is built in a random order, but thereafter it is modified very rarely. I do frequent selects from the table and in each select I need to order the query by the same column. Now is there a way to sort a table permanently by a column so that it does not need to be done again for each select?
You can add an index sorted by the column you want. The data will be presorted according to that index.
You can have only one place where you define it, and re-use that for
every query:
def base_query(session, what_for):
return session.query(what_for).order_by(what_for.rank_or_whatever)
Expand that as needed, then for all but very complex queries you can use that like so:
some_query = base_query(session(), Employee).filter(Employee.feet > 3)
The resulting query will be ordered by Employee.rank_or_whatever. If you are always querying for the same, You won't habve to use it as argument, of course.
EDIT: If you could somehow define a "permanent" order on your table which is observed by the engine without being issued an ORDER BY I'd think this would be an implementation feature specific to which RDBMS you use, and just convenience. Internally it makes for a DBMS no sense to being coerced how to store the data, since retrieving this data in a specific order is easily and efficiently accomplished by using an INDEX - forcing a specific order would probably decrease overall performance.
Does SQL have a function similar to the range function in python that will generate a set of increasing numerics? I am aware of the identity function, but I don't want to keep on creating and recreating tables just so I can get a set of increasing numbers.
Ultimately, I want to be able to dynamically create a range of numbers based on the results of a search function, eg
count(*) from teams where team= 'knicks'
would give me a number, say 20
an then I dynamically use that number as part of a function
function(20) ---> 1,2,3,4,5,6... 20
I want to use the result of the function to have a sorted table where each number corresponds to a player. I can't use identity here, because I'm keeping all the teams in one table, and I'll use the team name to pull out numbered team lists.
Still shakey on how to use stackoverflow's designing, so please bear with.
Try SQL SEQUENCE. For example this guide.
/edit: i´ve read it again, and maybe you should try to make better design of DB. Use normalizations... I don't really understand this sentence:
I want to use the result of the function to have a sorted table where
each number corresponds to a player.