I'm developing iOS app, using Swift, and I need to be able to get the posts based on how close they are to a certain location, and sort them based on when they were posted.

I know how to check how close one item's post location is to another location, but, my problem is that I need to get all of the posts within a x miles of the user.

Would it be better to scan the table, which as I understand, selects every single value from the database, and then check if every item returned is within x miles of the user, and seems resource-intensive?

Or, would it be better to create another table that has a static hash key, and set two local secondary indexes, one for the latitude, and one for the longitude, and just query for that one static hash key, and query where the latitude is between x and y, and the longitude is between a and b?

The AWS DynamoDB documentation warns agains using a hash key that is accessed too much:

This would be in a table with the following values for a static hash key, where Post ID is the ID of the actual post.

**static hash key** | **latitude (local secondary index)** | **longitude (local secondary index)** | **dateCreated (local secondary index) | **Post ID**

and for the scan option:

**ID** | **latitude ** | **longitude ** | **date created ** | **poster** | **comments** | **flags** | **upvotes** | **downvotes** | **popularity** 

Would having a static key be better than scanning a table performance-wise? How about cost-wise?

Related posts

Recent Viewed